| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/examples/widget_example.h" | 5 #include "ui/views/examples/widget_example.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "ui/views/controls/button/text_button.h" | 8 #include "ui/views/controls/button/text_button.h" |
| 9 #include "ui/views/layout/box_layout.h" | 9 #include "ui/views/layout/box_layout.h" |
| 10 #include "ui/views/layout/layout_manager.h" | |
| 11 #include "ui/views/view.h" | 10 #include "ui/views/view.h" |
| 12 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 12 #include "ui/views/window/dialog_delegate.h" |
| 13 | 13 |
| 14 namespace views { | 14 namespace views { |
| 15 namespace examples { | 15 namespace examples { |
| 16 namespace { | |
| 17 | |
| 18 // A layout manager that layouts a single child at | |
| 19 // the center of the host view. | |
| 20 class CenterLayout : public LayoutManager { | |
| 21 public: | |
| 22 CenterLayout() {} | |
| 23 virtual ~CenterLayout() {} | |
| 24 | |
| 25 // Overridden from LayoutManager: | |
| 26 virtual void Layout(View* host) { | |
| 27 View* child = host->child_at(0); | |
| 28 gfx::Size size = child->GetPreferredSize(); | |
| 29 child->SetBounds((host->width() - size.width()) / 2, | |
| 30 (host->height() - size.height()) / 2, | |
| 31 size.width(), size.height()); | |
| 32 } | |
| 33 | |
| 34 virtual gfx::Size GetPreferredSize(View* host) { | |
| 35 return gfx::Size(); | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 DISALLOW_COPY_AND_ASSIGN(CenterLayout); | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | 16 |
| 44 WidgetExample::WidgetExample() : ExampleBase("Widget") { | 17 WidgetExample::WidgetExample() : ExampleBase("Widget") { |
| 45 } | 18 } |
| 46 | 19 |
| 47 WidgetExample::~WidgetExample() { | 20 WidgetExample::~WidgetExample() { |
| 48 } | 21 } |
| 49 | 22 |
| 50 void WidgetExample::CreateExampleView(View* container) { | 23 void WidgetExample::CreateExampleView(View* container) { |
| 51 container->SetLayoutManager( | 24 container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 2)); |
| 52 new BoxLayout(BoxLayout::kHorizontal, 0, 0, 2)); | 25 BuildButton(container, "Popup widget", POPUP); |
| 53 BuildButton(container, "Create a popup widget", POPUP); | 26 BuildButton(container, "Dialog widget", DIALOG); |
| 54 BuildButton(container, "Create a transparent popup widget", | |
| 55 TRANSPARENT_POPUP); | |
| 56 #if defined(OS_LINUX) | 27 #if defined(OS_LINUX) |
| 57 View* vert_container = new View(); | 28 // Windows does not support TYPE_CONTROL top-level widgets. |
| 58 container->AddChildView(vert_container); | 29 BuildButton(container, "Child widget", CHILD); |
| 59 vert_container->SetLayoutManager( | |
| 60 new BoxLayout(BoxLayout::kVertical, 0, 0, 20)); | |
| 61 BuildButton(vert_container, "Create a child widget", CHILD); | |
| 62 BuildButton(vert_container, "Create a transparent child widget", | |
| 63 TRANSPARENT_CHILD); | |
| 64 #endif | 30 #endif |
| 65 } | 31 } |
| 66 | 32 |
| 67 void WidgetExample::BuildButton(View* container, | 33 void WidgetExample::BuildButton(View* container, |
| 68 const std::string& label, | 34 const std::string& label, |
| 69 int tag) { | 35 int tag) { |
| 70 TextButton* button = new TextButton(this, ASCIIToUTF16(label)); | 36 TextButton* button = new TextButton(this, ASCIIToUTF16(label)); |
| 71 button->set_tag(tag); | 37 button->set_tag(tag); |
| 72 container->AddChildView(button); | 38 container->AddChildView(button); |
| 73 } | 39 } |
| 74 | 40 |
| 75 void WidgetExample::InitWidget(Widget* widget, bool transparent) { | 41 void WidgetExample::ShowWidget(View* sender, Widget::InitParams params) { |
| 76 // Add view/native buttons to close the popup widget. | 42 // Setup shared Widget heirarchy and bounds parameters. |
| 77 TextButton* close_button = new TextButton( | 43 params.parent = sender->GetWidget()->GetNativeView(); |
| 78 this, ASCIIToUTF16("Close")); | 44 params.bounds = gfx::Rect(sender->GetBoundsInScreen().CenterPoint(), |
| 79 close_button->set_tag(CLOSE_WIDGET); | 45 gfx::Size(200, 100)); |
| 80 // TODO(oshima): support transparent native view. | |
| 81 NativeTextButton* native_button = new NativeTextButton( | |
| 82 this, ASCIIToUTF16("Native Close")); | |
| 83 native_button->set_tag(CLOSE_WIDGET); | |
| 84 | 46 |
| 85 View* button_container = new View(); | 47 Widget* widget = new Widget(); |
| 86 button_container->SetLayoutManager( | 48 widget->Init(params); |
| 87 new BoxLayout(BoxLayout::kHorizontal, 0, 0, 1)); | |
| 88 button_container->AddChildView(close_button); | |
| 89 button_container->AddChildView(native_button); | |
| 90 | 49 |
| 91 View* widget_container = new View(); | 50 // If the Widget has no contents by default, add a view with a 'Close' button. |
| 92 widget_container->SetLayoutManager(new CenterLayout); | 51 if (!widget->GetContentsView()) { |
| 93 widget_container->AddChildView(button_container); | 52 View* contents = new View(); |
| 94 | 53 contents->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); |
| 95 widget->SetContentsView(widget_container); | 54 contents->set_background(Background::CreateSolidBackground(SK_ColorGRAY)); |
| 96 | 55 BuildButton(contents, "Close", CLOSE_WIDGET); |
| 97 if (!transparent) { | 56 widget->SetContentsView(contents); |
| 98 widget_container->set_background( | |
| 99 Background::CreateStandardPanelBackground()); | |
| 100 } | 57 } |
| 101 | 58 |
| 102 // Show the widget. | |
| 103 widget->Show(); | 59 widget->Show(); |
| 104 } | 60 } |
| 105 | 61 |
| 106 #if defined(OS_LINUX) | |
| 107 void WidgetExample::CreateChild(View* parent, bool transparent) { | |
| 108 Widget* widget = new Widget; | |
| 109 // Compute where to place the child widget. | |
| 110 // We'll place it at the center of the root widget. | |
| 111 Widget* parent_widget = parent->GetWidget(); | |
| 112 gfx::Rect bounds = parent_widget->GetClientAreaBoundsInScreen(); | |
| 113 // Child widget is 200x200 square. | |
| 114 bounds.SetRect((bounds.width() - 200) / 2, (bounds.height() - 200) / 2, | |
| 115 200, 200); | |
| 116 // Initialize the child widget with the computed bounds. | |
| 117 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | |
| 118 params.transparent = transparent; | |
| 119 params.parent = parent_widget->GetNativeView(); | |
| 120 widget->Init(params); | |
| 121 InitWidget(widget, transparent); | |
| 122 } | |
| 123 #endif | |
| 124 | |
| 125 void WidgetExample::CreatePopup(View* parent, bool transparent) { | |
| 126 Widget* widget = new Widget; | |
| 127 | |
| 128 // Compute where to place the popup widget. | |
| 129 // We'll place it right below the create button. | |
| 130 gfx::Point point = parent->GetMirroredPosition(); | |
| 131 // The position in point is relative to the parent. Make it absolute. | |
| 132 View::ConvertPointToScreen(parent, &point); | |
| 133 // Add the height of create_button_. | |
| 134 point.Offset(0, parent->size().height()); | |
| 135 | |
| 136 // Initialize the popup widget with the computed bounds. | |
| 137 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
| 138 params.transparent = transparent; | |
| 139 params.parent = parent->GetWidget()->GetNativeView(); | |
| 140 params.bounds = gfx::Rect(point.x(), point.y(), 200, 300); | |
| 141 widget->Init(params); | |
| 142 InitWidget(widget, transparent); | |
| 143 } | |
| 144 | |
| 145 void WidgetExample::ButtonPressed(Button* sender, const ui::Event& event) { | 62 void WidgetExample::ButtonPressed(Button* sender, const ui::Event& event) { |
| 146 switch (sender->tag()) { | 63 switch (sender->tag()) { |
| 147 case POPUP: | 64 case POPUP: |
| 148 CreatePopup(sender, false); | 65 ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_POPUP)); |
| 149 break; | 66 break; |
| 150 case TRANSPARENT_POPUP: | 67 case DIALOG: { |
| 151 CreatePopup(sender, true); | 68 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); |
| 69 params.delegate = new DialogDelegateView(); |
| 70 ShowWidget(sender, params); |
| 152 break; | 71 break; |
| 153 #if defined(OS_LINUX) | 72 } |
| 154 case CHILD: | 73 case CHILD: |
| 155 CreateChild(sender, false); | 74 ShowWidget(sender, Widget::InitParams(Widget::InitParams::TYPE_CONTROL)); |
| 156 break; | 75 break; |
| 157 case TRANSPARENT_CHILD: | |
| 158 CreateChild(sender, true); | |
| 159 break; | |
| 160 #endif | |
| 161 case CLOSE_WIDGET: | 76 case CLOSE_WIDGET: |
| 162 sender->GetWidget()->Close(); | 77 sender->GetWidget()->Close(); |
| 163 break; | 78 break; |
| 164 } | 79 } |
| 165 } | 80 } |
| 166 | 81 |
| 167 } // namespace examples | 82 } // namespace examples |
| 168 } // namespace views | 83 } // namespace views |
| OLD | NEW |