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