| Index: ui/views/examples/widget_example.cc
|
| diff --git a/ui/views/examples/widget_example.cc b/ui/views/examples/widget_example.cc
|
| index f3ab5724dc025bba69ae0405a1317f855fbab016..112f57a98e358da89ddff0f333b1b32b01d3527d 100644
|
| --- a/ui/views/examples/widget_example.cc
|
| +++ b/ui/views/examples/widget_example.cc
|
| @@ -7,39 +7,12 @@
|
| #include "base/utf_string_conversions.h"
|
| #include "ui/views/controls/button/text_button.h"
|
| #include "ui/views/layout/box_layout.h"
|
| -#include "ui/views/layout/layout_manager.h"
|
| #include "ui/views/view.h"
|
| #include "ui/views/widget/widget.h"
|
| +#include "ui/views/window/dialog_delegate.h"
|
|
|
| namespace views {
|
| namespace examples {
|
| -namespace {
|
| -
|
| -// A layout manager that layouts a single child at
|
| -// the center of the host view.
|
| -class CenterLayout : public LayoutManager {
|
| - public:
|
| - CenterLayout() {}
|
| - virtual ~CenterLayout() {}
|
| -
|
| - // Overridden from LayoutManager:
|
| - virtual void Layout(View* host) {
|
| - View* child = host->child_at(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(View* host) {
|
| - return gfx::Size();
|
| - }
|
| -
|
| - private:
|
| - DISALLOW_COPY_AND_ASSIGN(CenterLayout);
|
| -};
|
| -
|
| -} // namespace
|
|
|
| WidgetExample::WidgetExample() : ExampleBase("Widget") {
|
| }
|
| @@ -48,19 +21,12 @@ WidgetExample::~WidgetExample() {
|
| }
|
|
|
| void WidgetExample::CreateExampleView(View* container) {
|
| - container->SetLayoutManager(
|
| - new BoxLayout(BoxLayout::kHorizontal, 0, 0, 2));
|
| - BuildButton(container, "Create a popup widget", POPUP);
|
| - BuildButton(container, "Create a transparent popup widget",
|
| - TRANSPARENT_POPUP);
|
| + container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 2));
|
| + BuildButton(container, "Popup widget", POPUP);
|
| + BuildButton(container, "Dialog widget", DIALOG);
|
| #if defined(OS_LINUX)
|
| - View* vert_container = new View();
|
| - container->AddChildView(vert_container);
|
| - vert_container->SetLayoutManager(
|
| - new BoxLayout(BoxLayout::kVertical, 0, 0, 20));
|
| - BuildButton(vert_container, "Create a child widget", CHILD);
|
| - BuildButton(vert_container, "Create a transparent child widget",
|
| - TRANSPARENT_CHILD);
|
| + // Windows does not support TYPE_CONTROL top-level widgets.
|
| + BuildButton(container, "Child widget", CHILD);
|
| #endif
|
| }
|
|
|
| @@ -72,92 +38,41 @@ void WidgetExample::BuildButton(View* container,
|
| container->AddChildView(button);
|
| }
|
|
|
| -void WidgetExample::InitWidget(Widget* widget, bool transparent) {
|
| - // Add view/native buttons to close the popup widget.
|
| - TextButton* close_button = new TextButton(
|
| - this, ASCIIToUTF16("Close"));
|
| - close_button->set_tag(CLOSE_WIDGET);
|
| - // TODO(oshima): support transparent native view.
|
| - NativeTextButton* native_button = new NativeTextButton(
|
| - this, ASCIIToUTF16("Native Close"));
|
| - native_button->set_tag(CLOSE_WIDGET);
|
| -
|
| - View* button_container = new View();
|
| - button_container->SetLayoutManager(
|
| - new BoxLayout(BoxLayout::kHorizontal, 0, 0, 1));
|
| - button_container->AddChildView(close_button);
|
| - button_container->AddChildView(native_button);
|
| -
|
| - View* widget_container = new View();
|
| - widget_container->SetLayoutManager(new CenterLayout);
|
| - widget_container->AddChildView(button_container);
|
| +void WidgetExample::ShowWidget(View* sender, Widget::InitParams params) {
|
| + // Setup shared Widget heirarchy and bounds parameters.
|
| + params.parent = sender->GetWidget()->GetNativeView();
|
| + params.bounds = gfx::Rect(sender->GetBoundsInScreen().CenterPoint(),
|
| + gfx::Size(200, 100));
|
|
|
| - widget->SetContentsView(widget_container);
|
| + Widget* widget = new Widget();
|
| + widget->Init(params);
|
|
|
| - if (!transparent) {
|
| - widget_container->set_background(
|
| - Background::CreateStandardPanelBackground());
|
| + // If the Widget has no contents by default, add a view with a 'Close' button.
|
| + if (!widget->GetContentsView()) {
|
| + View* contents = new View();
|
| + contents->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
|
| + contents->set_background(Background::CreateSolidBackground(SK_ColorGRAY));
|
| + BuildButton(contents, "Close", CLOSE_WIDGET);
|
| + widget->SetContentsView(contents);
|
| }
|
|
|
| - // Show the widget.
|
| widget->Show();
|
| }
|
|
|
| -#if defined(OS_LINUX)
|
| -void WidgetExample::CreateChild(View* parent, bool transparent) {
|
| - Widget* widget = new Widget;
|
| - // Compute where to place the child widget.
|
| - // We'll place it at the center of the root widget.
|
| - Widget* parent_widget = parent->GetWidget();
|
| - gfx::Rect bounds = parent_widget->GetClientAreaBoundsInScreen();
|
| - // 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::InitParams params(Widget::InitParams::TYPE_CONTROL);
|
| - params.transparent = transparent;
|
| - params.parent = parent_widget->GetNativeView();
|
| - widget->Init(params);
|
| - InitWidget(widget, transparent);
|
| -}
|
| -#endif
|
| -
|
| -void WidgetExample::CreatePopup(View* parent, bool transparent) {
|
| - Widget* widget = new Widget;
|
| -
|
| - // Compute where to place the popup widget.
|
| - // We'll place it right below the create button.
|
| - gfx::Point point = parent->GetMirroredPosition();
|
| - // The position in point is relative to the parent. Make it absolute.
|
| - View::ConvertPointToScreen(parent, &point);
|
| - // Add the height of create_button_.
|
| - point.Offset(0, parent->size().height());
|
| -
|
| - // Initialize the popup widget with the computed bounds.
|
| - Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
|
| - params.transparent = transparent;
|
| - params.parent = parent->GetWidget()->GetNativeView();
|
| - params.bounds = gfx::Rect(point.x(), point.y(), 200, 300);
|
| - widget->Init(params);
|
| - InitWidget(widget, transparent);
|
| -}
|
| -
|
| void WidgetExample::ButtonPressed(Button* sender, const ui::Event& event) {
|
| switch (sender->tag()) {
|
| case POPUP:
|
| - CreatePopup(sender, false);
|
| + ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_POPUP));
|
| break;
|
| - case TRANSPARENT_POPUP:
|
| - CreatePopup(sender, true);
|
| + case DIALOG: {
|
| + Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
|
| + params.delegate = new DialogDelegateView();
|
| + ShowWidget(sender, params);
|
| break;
|
| -#if defined(OS_LINUX)
|
| + }
|
| case CHILD:
|
| - CreateChild(sender, false);
|
| + ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_CONTROL));
|
| break;
|
| - case TRANSPARENT_CHILD:
|
| - CreateChild(sender, true);
|
| - break;
|
| -#endif
|
| case CLOSE_WIDGET:
|
| sender->GetWidget()->Close();
|
| break;
|
|
|