diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 02:07:30 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 02:07:30 +0000 |
commit | fc06e1a43487b3ac9e35f5429db7477398cfdf8d (patch) | |
tree | 578a14d70dc5a9b936093192000fc9cf0810e5ea /views | |
parent | 94b921421fed742750a07e3be58e010b847a0f3e (diff) | |
download | chromium_src-fc06e1a43487b3ac9e35f5429db7477398cfdf8d.zip chromium_src-fc06e1a43487b3ac9e35f5429db7477398cfdf8d.tar.gz chromium_src-fc06e1a43487b3ac9e35f5429db7477398cfdf8d.tar.bz2 |
Add views/examples/menu_example.h
The example demonstrates how to create a menu with regular, radio, and
check items. The example revealed a bug: http://crbug.com/30310.
I have a separate change to fix the bug.
TEST=try; manually
BUG=30310
Review URL: http://codereview.chromium.org/495017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34640 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r-- | views/examples/examples_main.cc | 9 | ||||
-rw-r--r-- | views/examples/menu_example.h | 199 | ||||
-rw-r--r-- | views/views.gyp | 2 |
3 files changed, 209 insertions, 1 deletions
diff --git a/views/examples/examples_main.cc b/views/examples/examples_main.cc index 81137c59..93bbaf4 100644 --- a/views/examples/examples_main.cc +++ b/views/examples/examples_main.cc @@ -14,6 +14,7 @@ #include "views/examples/button_example.h" #include "views/examples/combobox_example.h" #include "views/examples/message_box_example.h" +#include "views/examples/menu_example.h" #include "views/examples/radio_button_example.h" #include "views/examples/scroll_view_example.h" // Slider is not yet ported to Windows. @@ -80,8 +81,10 @@ void ExamplesMain::Run() { layout->StartRow(0 /* no expand */, 0); layout->AddView(status_label_); + // TODO(satorux): The window is getting wide. Eventually, we would have + // the second tabbed pane. views::Window* window = - views::Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 750, 300), this); + views::Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 850, 300), this); examples::TextfieldExample textfield_example(this); tabbed_pane->AddTab(textfield_example.GetExampleTitle(), @@ -131,6 +134,10 @@ void ExamplesMain::Run() { slider_example.GetExampleView()); #endif + examples::MenuExample menu_example(this); + tabbed_pane->AddTab(menu_example.GetExampleTitle(), + menu_example.GetExampleView()); + window->Show(); views::AcceleratorHandler accelerator_handler; MessageLoopForUI::current()->Run(&accelerator_handler); diff --git a/views/examples/menu_example.h b/views/examples/menu_example.h new file mode 100644 index 0000000..80e276d --- /dev/null +++ b/views/examples/menu_example.h @@ -0,0 +1,199 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef VIEWS_EXAMPLES_MENU_EXAMPLE_H_ +#define VIEWS_EXAMPLES_MENU_EXAMPLE_H_ + +#include <set> + +#include "app/menus/simple_menu_model.h" +#include "views/controls/button/menu_button.h" +#include "views/controls/menu/menu_2.h" +#include "views/controls/menu/view_menu_delegate.h" +#include "views/controls/button/text_button.h" +#include "views/examples/example_base.h" +#include "views/fill_layout.h" + +namespace examples { + +class ExampleMenuContents : public menus::SimpleMenuModel, + public menus::SimpleMenuModel::Delegate { + enum { + kGroupMakeDecision, + }; + + enum { + kCommandDoSomething, + kCommandSelectAscii, + kCommandSelectUtf8, + kCommandSelectUtf16, + kCommandCheckApple, + kCommandCheckOrange, + kCommandCheckKiwi, + kCommandGoHome, + }; + + public: + ExampleMenuContents() : + ALLOW_THIS_IN_INITIALIZER_LIST(menus::SimpleMenuModel(this)), + current_encoding_command_id_(kCommandSelectAscii) { + AddItem(kCommandDoSomething, WideToUTF16(L"Do Something")); + AddSeparator(); + AddRadioItem(kCommandSelectAscii, + WideToUTF16(L"ASCII"), kGroupMakeDecision); + AddRadioItem(kCommandSelectUtf8, + WideToUTF16(L"UTF-8"), kGroupMakeDecision); + AddRadioItem(kCommandSelectUtf16, + WideToUTF16(L"UTF-16"), kGroupMakeDecision); + AddSeparator(); + AddCheckItem(kCommandCheckApple, WideToUTF16(L"Apple")); + AddCheckItem(kCommandCheckOrange, WideToUTF16(L"Orange")); + AddCheckItem(kCommandCheckKiwi, WideToUTF16(L"Kiwi")); + AddSeparator(); + AddItem(kCommandGoHome, WideToUTF16(L"Go Home")); + menu_.reset(new views::Menu2(this)); + } + + void RunMenuAt(const gfx::Point& point) { + menu_->RunMenuAt(point, views::Menu2::ALIGN_TOPRIGHT); + } + + // menus::SimpleMenuModel::Delegate implementation. + virtual bool IsCommandIdChecked(int command_id) const { + // Radio items. + if (command_id == current_encoding_command_id_) { + return true; + } + + // Check items. + if (checked_fruits_.find(command_id) != checked_fruits_.end()) { + return true; + } + return false; + } + + virtual bool IsCommandIdEnabled(int command_id) const { + // All commands are enabled except for kCommandGoHome. + return command_id != kCommandGoHome; + } + + virtual bool GetAcceleratorForCommandId( + int command_id, + menus::Accelerator* accelerator) { + // We don't use this in the example. + return false; + } + + virtual void ExecuteCommand(int command_id) { + switch (command_id) { + case kCommandDoSomething: { + LOG(INFO) << "Done something"; + break; + } + + // Radio items. + case kCommandSelectAscii: { + current_encoding_command_id_ = kCommandSelectAscii; + LOG(INFO) << "Selected ASCII"; + break; + } + case kCommandSelectUtf8: { + current_encoding_command_id_ = kCommandSelectUtf8; + LOG(INFO) << "Selected UTF-8"; + break; + } + case kCommandSelectUtf16: { + current_encoding_command_id_ = kCommandSelectUtf16; + LOG(INFO) << "Selected UTF-16"; + break; + } + + // Check items. + case kCommandCheckApple: + case kCommandCheckOrange: + case kCommandCheckKiwi: { + // Print what fruit is checked. + const char* checked_fruit = ""; + if (command_id == kCommandCheckApple) { + checked_fruit = "Apple"; + } else if (command_id == kCommandCheckOrange) { + checked_fruit = "Orange"; + } else if (command_id == kCommandCheckKiwi) { + checked_fruit = "Kiwi"; + } + LOG(INFO) << "Checked " << checked_fruit; + + // Update the check status. + std::set<int>::iterator iter = checked_fruits_.find(command_id); + if (iter == checked_fruits_.end()) { + checked_fruits_.insert(command_id); + } else { + checked_fruits_.erase(iter); + } + break; + } + } + } + + private: + scoped_ptr<views::Menu2> menu_; + std::set<int> checked_fruits_; + int current_encoding_command_id_; + + DISALLOW_COPY_AND_ASSIGN(ExampleMenuContents); +}; + +class ExampleMenuButton : public views::MenuButton, + public views::ViewMenuDelegate { + public: + ExampleMenuButton(const std::wstring& test, + bool show_menu_marker) + : ALLOW_THIS_IN_INITIALIZER_LIST( + views::MenuButton(NULL, test, this, show_menu_marker)) { + } + + private: + // views::ViewMenuDelegate implementation. + virtual void RunMenu(views::View* source, const gfx::Point& point) { + if (menu_contents_ == NULL) { + menu_contents_.reset(new ExampleMenuContents); + } + menu_contents_->RunMenuAt(point); + } + + scoped_ptr<ExampleMenuContents> menu_contents_; + DISALLOW_COPY_AND_ASSIGN(ExampleMenuButton); +}; + +// MenuExample demonstrates how to use the Menu class. +class MenuExample : public ExampleBase { + public: + explicit MenuExample(ExamplesMain* main) + : ExampleBase(main) { + } + + virtual ~MenuExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Menu"; + } + + virtual void CreateExampleView(views::View* container) { + // views::Menu2 is not a sub class of View, hence we cannot directly + // add to the continer. Instead, we add a button to open a menu. + const bool show_menu_marker = true; + ExampleMenuButton* menu_button = new ExampleMenuButton( + L"Open a menu", show_menu_marker); + container->SetLayoutManager(new views::FillLayout); + container->AddChildView(menu_button); + } + + private: + DISALLOW_COPY_AND_ASSIGN(MenuExample); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_MENU_EXAMPLE_H_ + diff --git a/views/views.gyp b/views/views.gyp index dc0de4d..c48e5de 100644 --- a/views/views.gyp +++ b/views/views.gyp @@ -364,8 +364,10 @@ 'examples/examples_main.cc', 'examples/examples_main.h', 'examples/message_box_example.h', + 'examples/menu_example.h', 'examples/radio_button_example.h', 'examples/scroll_view_example.h', + 'examples/slider_example.h', 'examples/tabbed_pane_example.h', 'examples/textfield_example.h', 'examples/widget_example.h', |