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