diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-13 16:53:24 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-13 16:53:24 +0000 |
commit | c45ce15766d2902f807b63a3926dafd00ce90746 (patch) | |
tree | b5377d36df1df5182a937d98e5d6786a147d42a2 /views | |
parent | b509ec89a63c234c5e366ccee7b287150e337b6b (diff) | |
download | chromium_src-c45ce15766d2902f807b63a3926dafd00ce90746.zip chromium_src-c45ce15766d2902f807b63a3926dafd00ce90746.tar.gz chromium_src-c45ce15766d2902f807b63a3926dafd00ce90746.tar.bz2 |
Add more variations to Widget example.
Transparent POPUP, CHILD and Transparent child example has been added.
Transparent child is not working yet (it will crash)
Changed ScrollView example so that scrollable view has view components
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/1521025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44365 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r-- | views/examples/scroll_view_example.h | 13 | ||||
-rw-r--r-- | views/examples/widget_example.h | 222 |
2 files changed, 180 insertions, 55 deletions
diff --git a/views/examples/scroll_view_example.h b/views/examples/scroll_view_example.h index e183b2c..c644f62 100644 --- a/views/examples/scroll_view_example.h +++ b/views/examples/scroll_view_example.h @@ -7,6 +7,7 @@ #include "base/compiler_specific.h" #include "base/string_util.h" +#include "views/controls/button/radio_button.h" #include "views/controls/button/text_button.h" #include "views/controls/scroll_view.h" #include "views/examples/example_base.h" @@ -67,9 +68,11 @@ class ScrollViewExample : public ExampleBase, public: ScrollableView() { SetColor(SK_ColorRED, SK_ColorCYAN); + AddChildView(new views::TextButton(NULL, L"Button")); + AddChildView(new views::RadioButton(L"Radio Button", 0)); } - gfx::Size GetPreferredSize() { + virtual gfx::Size GetPreferredSize() { return gfx::Size(width(), height()); } @@ -78,7 +81,15 @@ class ScrollViewExample : public ExampleBase, views::Background::CreateVerticalGradientBackground(from, to)); } + void PlaceChildY(int index, int y) { + views::View* view = GetChildViewAt(index); + gfx::Size size = view->GetPreferredSize(); + view->SetBounds(0, y, size.width(), size.height()); + } + virtual void Layout() { + PlaceChildY(0, 0); + PlaceChildY(1, height() / 2); SizeToPreferredSize(); } diff --git a/views/examples/widget_example.h b/views/examples/widget_example.h index 85a7111..609e6ed 100644 --- a/views/examples/widget_example.h +++ b/views/examples/widget_example.h @@ -13,8 +13,75 @@ #include "views/widget/root_view.h" #include "views/widget/widget.h" +#if defined(OS_LINUX) +#include "views/widget/widget_gtk.h" +#endif + +namespace { + +// A layout manager that layouts child views vertically. +// TODO(oshima): support horizontal support and +// move to views/. +class BoxLayout : public views::LayoutManager { + public: + BoxLayout() {} + virtual ~BoxLayout() { + } + + // Overridden from LayoutManager: + virtual void Layout(views::View* host) { + int height = host->height(); + int width = host->width(); + int count = host->GetChildViewCount(); + + int y = 0; + for (int i = 0; i < count; i++) { + views::View* child = host->GetChildViewAt(i); + child->SetBounds(0, y, width, height / count); + y = height * (i + 1)/ count; + } + } + + virtual gfx::Size GetPreferredSize(views::View* host) { + return host->GetPreferredSize(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(BoxLayout); +}; + +// A layout manager that layouts a single child at +// the center of the host view. +class CenterLayout : public views::LayoutManager { + public: + CenterLayout() { + } + virtual ~CenterLayout() { + } + + // Overridden from LayoutManager: + virtual void Layout(views::View* host) { + views::View* child = host->GetChildViewAt(0); + gfx::Size size = child->GetPreferredSize(); + child->SetBounds((host->width() - size.width()) / 2, + (host->height() - size.height()) / 2, + size.width(), size.height()); + } + + virtual gfx::Size GetPreferredSize(views::View* host) { + return host->GetPreferredSize(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(CenterLayout); +}; + +} // namespace + namespace examples { +using views::Widget; + // WidgetExample demonstrates how to create a popup widget. class WidgetExample : public ExampleBase, public views::ButtonListener { public: @@ -28,71 +95,118 @@ class WidgetExample : public ExampleBase, public views::ButtonListener { } 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; + container->SetLayoutManager(new BoxLayout()); + BuildButton(container, L"Create a popup widget", POPUP); + BuildButton(container, L"Create a transparent popup widget", + TRANSPARENT_POPUP); +#if defined(OS_LINUX) + BuildButton(container, L"Create a child widget", CHILD); + BuildButton(container, + L"Create a transparent child widget", TRANSPARENT_CHILD); +#endif } private: + enum Command { + POPUP, + CHILD, + TRANSPARENT_POPUP, + TRANSPARENT_CHILD, + CLOSE_WIDGET, + }; + + void BuildButton(views::View* container, const std::wstring& label, int tag) { + views::TextButton* button = new views::TextButton(this, label); + button->set_tag(tag); + container->AddChildView(button); + } + + void InitWidget(Widget* widget, + const Widget::TransparencyParam transparency) { + // Add a button to close the popup widget. + views::TextButton* close_button = new views::TextButton(this, L"Close"); + close_button->set_tag(CLOSE_WIDGET); + views::View* widget_container = new views::View(); + widget_container->SetLayoutManager(new CenterLayout); + if (transparency == Widget::Transparent) + close_button->set_background( + views::Background::CreateStandardPanelBackground()); + else + widget_container->set_background( + views::Background::CreateStandardPanelBackground()); + widget_container->AddChildView(close_button); + widget->GetRootView()->SetContentsView(widget_container); + + // Show the widget. + widget->Show(); + } + +#if defined(OS_LINUX) + void CreateChild(views::View* parent, + const Widget::TransparencyParam transparency) { + views::WidgetGtk* widget = + new views::WidgetGtk(views::WidgetGtk::TYPE_CHILD); + if (transparency == Widget::Transparent) + widget->MakeTransparent(); + // Compute where to place the child widget. + // We'll place it at the center of the root widget. + views::WidgetGtk* parent_widget = + static_cast<views::WidgetGtk*>(parent->GetWidget()->GetRootWidget()); + gfx::Rect bounds; + parent_widget->GetBounds(&bounds, false); + // Child widget is 200x200 square. + bounds.SetRect((bounds.width() - 200) / 2, (bounds.height() - 200) / 2, + 200, 200); + // Initialize the child widget with the computed bounds. + widget->Init(parent_widget->window_contents(), bounds); + InitWidget(widget, transparency); + } +#endif + + void CreatePopup(views::View* parent, + const Widget::TransparencyParam transparency) { + Widget* widget = Widget::CreatePopupWidget(transparency, + Widget::AcceptEvents, + Widget::DeleteOnDestroy); + // Compute where to place the popup widget. + // We'll place it right below the create button. + gfx::Point point = parent->GetPosition(); + // The position in point is relative to the parent. Make it absolute. + views::View::ConvertPointToScreen(parent, &point); + // Add the height of create_button_. + point.Offset(0, parent->size().height()); + gfx::Rect bounds(point.x(), point.y(), 200, 300); + // Initialize the popup widget with the computed bounds. + widget->Init(NULL, bounds); + InitWidget(widget, transparency); + } + // 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); - - // Compute where to place the popup widget. - // We'll place it right below the create_button_. - gfx::Point point = create_button_->GetPosition(); - // The position in point is relative to the parent. Make it absolute. - views::View::ConvertPointToScreen(create_button_, &point); - // Add the height of create_button_. - point.Offset(0, create_button_->size().height()); - gfx::Rect bounds(point.x(), point.y(), 200, 300); - - // Initialize the popup widget with the computed bounds. - popup_widget_->Init(NULL, bounds); - - // 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; + switch (sender->tag()) { + case POPUP: + CreatePopup(sender, Widget::NotTransparent); + break; + case TRANSPARENT_POPUP: + CreatePopup(sender, Widget::Transparent); + break; +#if defined(OS_LINUX) + case CHILD: + CreateChild(sender, Widget::NotTransparent); + break; + case TRANSPARENT_CHILD: + CreateChild(sender, Widget::Transparent); + break; +#endif + case CLOSE_WIDGET: + sender->GetWidget()->Close(); + break; } } - 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_ - |