diff options
author | satorux@google.com <satorux@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 04:04:26 +0000 |
---|---|---|
committer | satorux@google.com <satorux@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 04:04:26 +0000 |
commit | e5e161de9c054e53bf1f98966aff0a07719d3f6f (patch) | |
tree | cf3e79c0a5c89ae6a93bf9697e2ab6104ca3b642 /views | |
parent | 049991792da2227508aaeef253a40408451893bd (diff) | |
download | chromium_src-e5e161de9c054e53bf1f98966aff0a07719d3f6f.zip chromium_src-e5e161de9c054e53bf1f98966aff0a07719d3f6f.tar.gz chromium_src-e5e161de9c054e53bf1f98966aff0a07719d3f6f.tar.bz2 |
Add views/examples/widget_example.h.
The example demonstrates how to create a popup widget.
BUG=none
TEST=manually tested on a linux machine.
Review URL: http://codereview.chromium.org/406001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32490 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r-- | views/examples/examples_main.cc | 7 | ||||
-rw-r--r-- | views/examples/widget_example.h | 87 |
2 files changed, 93 insertions, 1 deletions
diff --git a/views/examples/examples_main.cc b/views/examples/examples_main.cc index 9aa2adf..badef5c 100644 --- a/views/examples/examples_main.cc +++ b/views/examples/examples_main.cc @@ -23,6 +23,7 @@ #include "views/examples/table2_example.h" #endif #include "views/examples/textfield_example.h" +#include "views/examples/widget_example.h" #include "views/focus/accelerator_handler.h" #include "views/grid_layout.h" #include "views/window/window.h" @@ -78,7 +79,7 @@ void ExamplesMain::Run() { layout->AddView(status_label_); views::Window* window = - views::Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 600, 300), this); + views::Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 700, 300), this); examples::TextfieldExample textfield_example(this); tabbed_pane->AddTab(textfield_example.GetExampleTitle(), @@ -119,6 +120,10 @@ void ExamplesMain::Run() { table2_example.GetExampleView()); #endif + examples::WidgetExample widget_example(this); + tabbed_pane->AddTab(widget_example.GetExampleTitle(), + widget_example.GetExampleView()); + window->Show(); views::AcceleratorHandler accelerator_handler; MessageLoopForUI::current()->Run(&accelerator_handler); diff --git a/views/examples/widget_example.h b/views/examples/widget_example.h new file mode 100644 index 0000000..d7af468 --- /dev/null +++ b/views/examples/widget_example.h @@ -0,0 +1,87 @@ +// 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_WIDGET_EXAMPLE_H_ +#define VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ + +#include "views/background.h" +#include "views/controls/button/text_button.h" +#include "views/examples/example_base.h" +#include "views/fill_layout.h" +#include "views/view.h" +#include "views/widget/root_view.h" +#include "views/widget/widget.h" + +namespace examples { + +// WidgetExample demonstrates how to create a popup widget. +class WidgetExample : public ExampleBase, public views::ButtonListener { + public: + explicit WidgetExample(ExamplesMain* main) : ExampleBase(main) { + } + + virtual ~WidgetExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Widget"; + } + + virtual void CreateExampleView(views::View* container) { + create_button_ = new views::TextButton(this, L"Create a popup widget"); + container->SetLayoutManager(new views::FillLayout); + container->AddChildView(create_button_); + // We'll create close_button_ and popup_widget_, when the create button + // is clicked. + close_button_ = NULL; + popup_widget_ = NULL; + } + + private: + // ButtonListner implementation. + virtual void ButtonPressed(views::Button* sender, const views::Event& event) { + if (sender == create_button_) { + // Disable the create button. + create_button_->SetEnabled(false); + + // Create a popup widget. + popup_widget_ = views::Widget::CreatePopupWidget( + views::Widget::NotTransparent, + views::Widget::AcceptEvents, + views::Widget::DeleteOnDestroy); + popup_widget_->Init(NULL, gfx::Rect(100, 100, 200, 300)); + + // Add a button to close the popup widget. + close_button_ = new views::TextButton(this, L"Close"); + views::View* widget_container = new views::View; + widget_container->SetLayoutManager(new views::FillLayout); + widget_container->set_background( + views::Background::CreateStandardPanelBackground()); + widget_container->AddChildView(close_button_); + popup_widget_->GetRootView()->SetContentsView(widget_container); + + // Show the popup widget. + popup_widget_->Show(); + } else if (sender == close_button_) { + // Close the popup widget. This will delete popup_widget_ as + // DeleteOnDestroy is specified when the widget was created. + // Views on the widget will also be deleted. + popup_widget_->Close(); + // Re-enable the create button. + create_button_->SetEnabled(true); + close_button_ = NULL; + popup_widget_ = NULL; + } + } + + views::TextButton* create_button_; + views::TextButton* close_button_; + views::Widget* popup_widget_; + + DISALLOW_COPY_AND_ASSIGN(WidgetExample); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ + |