| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ | 5 #ifndef VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ |
| 6 #define VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ | 6 #define VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ |
| 7 | 7 |
| 8 #include "views/background.h" | 8 #include "views/background.h" |
| 9 #include "views/controls/button/text_button.h" | 9 #include "views/controls/button/text_button.h" |
| 10 #include "views/examples/box_layout.h" |
| 10 #include "views/examples/example_base.h" | 11 #include "views/examples/example_base.h" |
| 11 #include "views/fill_layout.h" | |
| 12 #include "views/view.h" | 12 #include "views/view.h" |
| 13 #include "views/widget/root_view.h" | 13 #include "views/widget/root_view.h" |
| 14 #include "views/widget/widget.h" | 14 #include "views/widget/widget.h" |
| 15 | 15 |
| 16 #if defined(OS_LINUX) | 16 #if defined(OS_LINUX) |
| 17 #include "views/widget/widget_gtk.h" | 17 #include "views/widget/widget_gtk.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // A layout manager that layouts child views vertically. | |
| 23 // TODO(oshima): support horizontal support and | |
| 24 // move to views/. | |
| 25 class BoxLayout : public views::LayoutManager { | |
| 26 public: | |
| 27 BoxLayout() {} | |
| 28 virtual ~BoxLayout() { | |
| 29 } | |
| 30 | |
| 31 // Overridden from LayoutManager: | |
| 32 virtual void Layout(views::View* host) { | |
| 33 int height = host->height(); | |
| 34 int width = host->width(); | |
| 35 int count = host->GetChildViewCount(); | |
| 36 | |
| 37 int y = 0; | |
| 38 for (int i = 0; i < count; i++) { | |
| 39 views::View* child = host->GetChildViewAt(i); | |
| 40 child->SetBounds(0, y, width, height / count); | |
| 41 y = height * (i + 1)/ count; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 virtual gfx::Size GetPreferredSize(views::View* host) { | |
| 46 int count = host->GetChildViewCount(); | |
| 47 gfx::Rect bounds; | |
| 48 int y = 0; | |
| 49 for (int i = 0; i < count; i++) { | |
| 50 views::View* child = host->GetChildViewAt(i); | |
| 51 gfx::Size size = child->GetPreferredSize(); | |
| 52 gfx::Rect child_bounds(0, y, size.width(), size.height()); | |
| 53 bounds = bounds.Union(child_bounds); | |
| 54 y += size.height(); | |
| 55 } | |
| 56 return bounds.size(); | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 DISALLOW_COPY_AND_ASSIGN(BoxLayout); | |
| 61 }; | |
| 62 | |
| 63 // A layout manager that layouts a single child at | 22 // A layout manager that layouts a single child at |
| 64 // the center of the host view. | 23 // the center of the host view. |
| 65 class CenterLayout : public views::LayoutManager { | 24 class CenterLayout : public views::LayoutManager { |
| 66 public: | 25 public: |
| 67 CenterLayout() { | 26 CenterLayout() {} |
| 68 } | 27 virtual ~CenterLayout() {} |
| 69 virtual ~CenterLayout() { | |
| 70 } | |
| 71 | 28 |
| 72 // Overridden from LayoutManager: | 29 // Overridden from LayoutManager: |
| 73 virtual void Layout(views::View* host) { | 30 virtual void Layout(views::View* host) { |
| 74 views::View* child = host->GetChildViewAt(0); | 31 views::View* child = host->GetChildViewAt(0); |
| 75 gfx::Size size = child->GetPreferredSize(); | 32 gfx::Size size = child->GetPreferredSize(); |
| 76 child->SetBounds((host->width() - size.width()) / 2, | 33 child->SetBounds((host->width() - size.width()) / 2, |
| 77 (host->height() - size.height()) / 2, | 34 (host->height() - size.height()) / 2, |
| 78 size.width(), size.height()); | 35 size.width(), size.height()); |
| 79 } | 36 } |
| 80 | 37 |
| 81 virtual gfx::Size GetPreferredSize(views::View* host) { | 38 virtual gfx::Size GetPreferredSize(views::View* host) { |
| 82 return host->GetPreferredSize(); | 39 return host->GetPreferredSize(); |
| 83 } | 40 } |
| 84 | 41 |
| 85 private: | 42 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(CenterLayout); | 43 DISALLOW_COPY_AND_ASSIGN(CenterLayout); |
| 87 }; | 44 }; |
| 88 | 45 |
| 89 } // namespace | 46 } // namespace |
| 90 | 47 |
| 91 namespace examples { | 48 namespace examples { |
| 92 | 49 |
| 93 using views::Widget; | 50 using views::Widget; |
| 94 | 51 |
| 95 // WidgetExample demonstrates how to create a popup widget. | 52 // WidgetExample demonstrates how to create a popup widget. |
| 96 class WidgetExample : public ExampleBase, public views::ButtonListener { | 53 class WidgetExample : public ExampleBase, public views::ButtonListener { |
| 97 public: | 54 public: |
| 98 explicit WidgetExample(ExamplesMain* main) : ExampleBase(main) { | 55 explicit WidgetExample(ExamplesMain* main) : ExampleBase(main) {} |
| 99 } | |
| 100 | 56 |
| 101 virtual ~WidgetExample() {} | 57 virtual ~WidgetExample() {} |
| 102 | 58 |
| 103 virtual std::wstring GetExampleTitle() { | 59 virtual std::wstring GetExampleTitle() { return L"Widget"; } |
| 104 return L"Widget"; | |
| 105 } | |
| 106 | 60 |
| 107 virtual void CreateExampleView(views::View* container) { | 61 virtual void CreateExampleView(views::View* container) { |
| 108 container->SetLayoutManager(new BoxLayout()); | 62 container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 2)); |
| 109 BuildButton(container, L"Create a popup widget", POPUP); | 63 BuildButton(container, L"Create a popup widget", POPUP); |
| 110 BuildButton(container, L"Create a transparent popup widget", | 64 BuildButton(container, L"Create a transparent popup widget", |
| 111 TRANSPARENT_POPUP); | 65 TRANSPARENT_POPUP); |
| 112 #if defined(OS_LINUX) | 66 #if defined(OS_LINUX) |
| 113 BuildButton(container, L"Create a child widget", CHILD); | 67 views::View* vert_container = new views::View(); |
| 114 BuildButton(container, | 68 container->AddChildView(vert_container); |
| 115 L"Create a transparent child widget", TRANSPARENT_CHILD); | 69 vert_container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 20)); |
| 70 BuildButton(vert_container, L"Create a child widget", CHILD); |
| 71 BuildButton(vert_container, L"Create a transparent child widget", |
| 72 TRANSPARENT_CHILD); |
| 116 #endif | 73 #endif |
| 117 } | 74 } |
| 118 | 75 |
| 119 private: | 76 private: |
| 120 enum Command { | 77 enum Command { |
| 121 POPUP, | 78 POPUP, |
| 122 CHILD, | 79 CHILD, |
| 123 TRANSPARENT_POPUP, | 80 TRANSPARENT_POPUP, |
| 124 TRANSPARENT_CHILD, | 81 TRANSPARENT_CHILD, |
| 125 CLOSE_WIDGET, | 82 CLOSE_WIDGET, |
| 126 }; | 83 }; |
| 127 | 84 |
| 128 void BuildButton(views::View* container, const std::wstring& label, int tag) { | 85 void BuildButton(views::View* container, const std::wstring& label, int tag) { |
| 129 views::TextButton* button = new views::TextButton(this, label); | 86 views::TextButton* button = new views::TextButton(this, label); |
| 130 button->set_tag(tag); | 87 button->set_tag(tag); |
| 131 container->AddChildView(button); | 88 container->AddChildView(button); |
| 132 } | 89 } |
| 133 | 90 |
| 134 void InitWidget(Widget* widget, | 91 void InitWidget(Widget* widget, |
| 135 const Widget::TransparencyParam transparency) { | 92 const Widget::TransparencyParam transparency) { |
| 136 // Add view/native buttons to close the popup widget. | 93 // Add view/native buttons to close the popup widget. |
| 137 views::TextButton* close_button = new views::TextButton(this, L"Close"); | 94 views::TextButton* close_button = new views::TextButton(this, L"Close"); |
| 138 close_button->set_tag(CLOSE_WIDGET); | 95 close_button->set_tag(CLOSE_WIDGET); |
| 139 // TODO(oshima): support transparent native view. | 96 // TODO(oshima): support transparent native view. |
| 140 views::NativeButton* native_button | 97 views::NativeButton* native_button |
| 141 = new views::NativeButton(this, L"Native Close"); | 98 = new views::NativeButton(this, L"Native Close"); |
| 142 native_button->set_tag(CLOSE_WIDGET); | 99 native_button->set_tag(CLOSE_WIDGET); |
| 143 | 100 |
| 144 views::View* button_container = new views::View(); | 101 views::View* button_container = new views::View(); |
| 145 button_container->SetLayoutManager(new BoxLayout); | 102 button_container->SetLayoutManager( |
| 103 new BoxLayout(BoxLayout::kHorizontal, 1)); |
| 146 button_container->AddChildView(close_button); | 104 button_container->AddChildView(close_button); |
| 147 button_container->AddChildView(native_button); | 105 button_container->AddChildView(native_button); |
| 148 | 106 |
| 149 views::View* widget_container = new views::View(); | 107 views::View* widget_container = new views::View(); |
| 150 widget_container->SetLayoutManager(new CenterLayout); | 108 widget_container->SetLayoutManager(new CenterLayout); |
| 151 widget_container->AddChildView(button_container); | 109 widget_container->AddChildView(button_container); |
| 152 | 110 |
| 153 widget->SetContentsView(widget_container); | 111 widget->SetContentsView(widget_container); |
| 154 | 112 |
| 155 if (transparency != Widget::Transparent) { | 113 if (transparency != Widget::Transparent) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 break; | 181 break; |
| 224 } | 182 } |
| 225 } | 183 } |
| 226 | 184 |
| 227 DISALLOW_COPY_AND_ASSIGN(WidgetExample); | 185 DISALLOW_COPY_AND_ASSIGN(WidgetExample); |
| 228 }; | 186 }; |
| 229 | 187 |
| 230 } // namespace examples | 188 } // namespace examples |
| 231 | 189 |
| 232 #endif // VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ | 190 #endif // VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_ |
| OLD | NEW |