Add ICU message format support
[chromium-blink-merge.git] / ui / views / examples / menu_example.cc
blob551de1b6c9ada60824eb57d689287bd5e8493d60
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
3 // found in the LICENSE file.
5 #include "ui/views/examples/menu_example.h"
7 #include <set>
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/base/models/simple_menu_model.h"
11 #include "ui/views/controls/button/menu_button.h"
12 #include "ui/views/controls/button/menu_button_listener.h"
13 #include "ui/views/controls/menu/menu_runner.h"
14 #include "ui/views/layout/fill_layout.h"
15 #include "ui/views/view.h"
16 #include "ui/views/widget/widget.h"
18 using base::ASCIIToUTF16;
20 namespace views {
21 namespace examples {
23 namespace {
25 class ExampleMenuModel : public ui::SimpleMenuModel,
26 public ui::SimpleMenuModel::Delegate {
27 public:
28 ExampleMenuModel();
30 // ui::SimpleMenuModel::Delegate:
31 bool IsCommandIdChecked(int command_id) const override;
32 bool IsCommandIdEnabled(int command_id) const override;
33 bool GetAcceleratorForCommandId(int command_id,
34 ui::Accelerator* accelerator) override;
35 void ExecuteCommand(int command_id, int event_flags) override;
37 private:
38 enum GroupID {
39 GROUP_MAKE_DECISION,
42 enum CommandID {
43 COMMAND_DO_SOMETHING,
44 COMMAND_SELECT_ASCII,
45 COMMAND_SELECT_UTF8,
46 COMMAND_SELECT_UTF16,
47 COMMAND_CHECK_APPLE,
48 COMMAND_CHECK_ORANGE,
49 COMMAND_CHECK_KIWI,
50 COMMAND_GO_HOME,
53 scoped_ptr<ui::SimpleMenuModel> submenu_;
54 std::set<int> checked_fruits_;
55 int current_encoding_command_id_;
57 DISALLOW_COPY_AND_ASSIGN(ExampleMenuModel);
60 class ExampleMenuButton : public MenuButton, public MenuButtonListener {
61 public:
62 explicit ExampleMenuButton(const base::string16& test);
63 ~ExampleMenuButton() override;
65 private:
66 // MenuButtonListener:
67 void OnMenuButtonClicked(View* source, const gfx::Point& point) override;
69 ui::SimpleMenuModel* GetMenuModel();
71 scoped_ptr<ExampleMenuModel> menu_model_;
72 scoped_ptr<MenuRunner> menu_runner_;
74 DISALLOW_COPY_AND_ASSIGN(ExampleMenuButton);
77 // ExampleMenuModel ---------------------------------------------------------
79 ExampleMenuModel::ExampleMenuModel()
80 : ui::SimpleMenuModel(this),
81 current_encoding_command_id_(COMMAND_SELECT_ASCII) {
82 AddItem(COMMAND_DO_SOMETHING, ASCIIToUTF16("Do Something"));
83 AddSeparator(ui::NORMAL_SEPARATOR);
84 AddRadioItem(COMMAND_SELECT_ASCII, ASCIIToUTF16("ASCII"),
85 GROUP_MAKE_DECISION);
86 AddRadioItem(COMMAND_SELECT_UTF8, ASCIIToUTF16("UTF-8"),
87 GROUP_MAKE_DECISION);
88 AddRadioItem(COMMAND_SELECT_UTF16, ASCIIToUTF16("UTF-16"),
89 GROUP_MAKE_DECISION);
90 AddSeparator(ui::NORMAL_SEPARATOR);
91 AddCheckItem(COMMAND_CHECK_APPLE, ASCIIToUTF16("Apple"));
92 AddCheckItem(COMMAND_CHECK_ORANGE, ASCIIToUTF16("Orange"));
93 AddCheckItem(COMMAND_CHECK_KIWI, ASCIIToUTF16("Kiwi"));
94 AddSeparator(ui::NORMAL_SEPARATOR);
95 AddItem(COMMAND_GO_HOME, ASCIIToUTF16("Go Home"));
97 submenu_.reset(new ui::SimpleMenuModel(this));
98 submenu_->AddItem(COMMAND_DO_SOMETHING, ASCIIToUTF16("Do Something 2"));
99 AddSubMenu(0, ASCIIToUTF16("Submenu"), submenu_.get());
102 bool ExampleMenuModel::IsCommandIdChecked(int command_id) const {
103 // Radio items.
104 if (command_id == current_encoding_command_id_)
105 return true;
107 // Check items.
108 if (checked_fruits_.find(command_id) != checked_fruits_.end())
109 return true;
111 return false;
114 bool ExampleMenuModel::IsCommandIdEnabled(int command_id) const {
115 // All commands are enabled except for COMMAND_GO_HOME.
116 return command_id != COMMAND_GO_HOME;
119 bool ExampleMenuModel::GetAcceleratorForCommandId(
120 int command_id,
121 ui::Accelerator* accelerator) {
122 // We don't use this in the example.
123 return false;
126 void ExampleMenuModel::ExecuteCommand(int command_id, int event_flags) {
127 switch (command_id) {
128 case COMMAND_DO_SOMETHING: {
129 VLOG(0) << "Done something";
130 break;
133 // Radio items.
134 case COMMAND_SELECT_ASCII: {
135 current_encoding_command_id_ = COMMAND_SELECT_ASCII;
136 VLOG(0) << "Selected ASCII";
137 break;
139 case COMMAND_SELECT_UTF8: {
140 current_encoding_command_id_ = COMMAND_SELECT_UTF8;
141 VLOG(0) << "Selected UTF-8";
142 break;
144 case COMMAND_SELECT_UTF16: {
145 current_encoding_command_id_ = COMMAND_SELECT_UTF16;
146 VLOG(0) << "Selected UTF-16";
147 break;
150 // Check items.
151 case COMMAND_CHECK_APPLE:
152 case COMMAND_CHECK_ORANGE:
153 case COMMAND_CHECK_KIWI: {
154 // Print what fruit is checked.
155 const char* checked_fruit = "";
156 if (command_id == COMMAND_CHECK_APPLE)
157 checked_fruit = "Apple";
158 else if (command_id == COMMAND_CHECK_ORANGE)
159 checked_fruit = "Orange";
160 else if (command_id == COMMAND_CHECK_KIWI)
161 checked_fruit = "Kiwi";
163 // Update the check status.
164 std::set<int>::iterator iter = checked_fruits_.find(command_id);
165 if (iter == checked_fruits_.end()) {
166 DVLOG(1) << "Checked " << checked_fruit;
167 checked_fruits_.insert(command_id);
168 } else {
169 DVLOG(1) << "Unchecked " << checked_fruit;
170 checked_fruits_.erase(iter);
172 break;
177 // ExampleMenuButton -----------------------------------------------------------
179 ExampleMenuButton::ExampleMenuButton(const base::string16& test)
180 : MenuButton(NULL, test, this, true) {
183 ExampleMenuButton::~ExampleMenuButton() {
186 void ExampleMenuButton::OnMenuButtonClicked(View* source,
187 const gfx::Point& point) {
188 menu_runner_.reset(new MenuRunner(GetMenuModel(), MenuRunner::HAS_MNEMONICS));
190 if (menu_runner_->RunMenuAt(source->GetWidget()->GetTopLevelWidget(),
191 this,
192 gfx::Rect(point, gfx::Size()),
193 MENU_ANCHOR_TOPRIGHT,
194 ui::MENU_SOURCE_NONE) ==
195 MenuRunner::MENU_DELETED) {
196 return;
200 ui::SimpleMenuModel* ExampleMenuButton::GetMenuModel() {
201 if (!menu_model_.get())
202 menu_model_.reset(new ExampleMenuModel);
203 return menu_model_.get();
206 } // namespace
208 MenuExample::MenuExample() : ExampleBase("Menu") {
211 MenuExample::~MenuExample() {
214 void MenuExample::CreateExampleView(View* container) {
215 // We add a button to open a menu.
216 ExampleMenuButton* menu_button = new ExampleMenuButton(
217 ASCIIToUTF16("Open a menu"));
218 container->SetLayoutManager(new FillLayout);
219 container->AddChildView(menu_button);
222 } // namespace examples
223 } // namespace views