Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(540)

Side by Side Diff: ui/views/examples/widget_example.cc

Issue 11571023: Move ash/wm's DialogFrameView to ui/views/window; etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove redundant GetContentsView() OVERRIDEs; re-git-add dialog_frame_view files. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
16 namespace { 17 namespace {
17 18
18 // A layout manager that layouts a single child at 19 Widget::InitParams GetPopupParams() {
19 // the center of the host view. 20 return Widget::InitParams(Widget::InitParams::TYPE_POPUP);
20 class CenterLayout : public LayoutManager { 21 }
21 public:
22 CenterLayout() {}
23 virtual ~CenterLayout() {}
24 22
25 // Overridden from LayoutManager: 23 Widget::InitParams GetDialogParams() {
26 virtual void Layout(View* host) { 24 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
27 View* child = host->child_at(0); 25 params.delegate = new DialogDelegateView();
28 gfx::Size size = child->GetPreferredSize(); 26 return params;
29 child->SetBounds((host->width() - size.width()) / 2, 27 }
30 (host->height() - size.height()) / 2,
31 size.width(), size.height());
32 }
33 28
34 virtual gfx::Size GetPreferredSize(View* host) { 29 Widget::InitParams GetChildParams() {
35 return gfx::Size(); 30 return Widget::InitParams(Widget::InitParams::TYPE_CONTROL);
36 } 31 }
37
38 private:
39 DISALLOW_COPY_AND_ASSIGN(CenterLayout);
40 };
41 32
42 } // namespace 33 } // namespace
43 34
44 WidgetExample::WidgetExample() : ExampleBase("Widget") { 35 WidgetExample::WidgetExample() : ExampleBase("Widget") {
45 } 36 }
46 37
47 WidgetExample::~WidgetExample() { 38 WidgetExample::~WidgetExample() {
48 } 39 }
49 40
50 void WidgetExample::CreateExampleView(View* container) { 41 void WidgetExample::CreateExampleView(View* container) {
51 container->SetLayoutManager( 42 container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 2));
52 new BoxLayout(BoxLayout::kHorizontal, 0, 0, 2)); 43 BuildButton(container, "Popup widget", POPUP);
53 BuildButton(container, "Create a popup widget", POPUP); 44 BuildButton(container, "Dialog widget", DIALOG);
54 BuildButton(container, "Create a transparent popup widget",
55 TRANSPARENT_POPUP);
56 #if defined(OS_LINUX) 45 #if defined(OS_LINUX)
57 View* vert_container = new View(); 46 // Windows does not support TYPE_CONTROL top-level widgets.
58 container->AddChildView(vert_container); 47 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 48 #endif
65 } 49 }
66 50
67 void WidgetExample::BuildButton(View* container, 51 void WidgetExample::BuildButton(View* container,
68 const std::string& label, 52 const std::string& label,
69 int tag) { 53 int tag) {
70 TextButton* button = new TextButton(this, ASCIIToUTF16(label)); 54 TextButton* button = new TextButton(this, ASCIIToUTF16(label));
71 button->set_tag(tag); 55 button->set_tag(tag);
72 container->AddChildView(button); 56 container->AddChildView(button);
73 } 57 }
74 58
75 void WidgetExample::InitWidget(Widget* widget, bool transparent) { 59 void WidgetExample::ShowWidget(View* sender, Widget::InitParams params) {
76 // Add view/native buttons to close the popup widget. 60 // Setup shared Widget heirarchy and bounds parameters.
77 TextButton* close_button = new TextButton( 61 params.parent = sender->GetWidget()->GetNativeView();
78 this, ASCIIToUTF16("Close")); 62 params.bounds = gfx::Rect(sender->GetBoundsInScreen().CenterPoint(),
79 close_button->set_tag(CLOSE_WIDGET); 63 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 64
85 View* button_container = new View(); 65 Widget* widget = new Widget();
86 button_container->SetLayoutManager( 66 widget->Init(params);
87 new BoxLayout(BoxLayout::kHorizontal, 0, 0, 1));
88 button_container->AddChildView(close_button);
89 button_container->AddChildView(native_button);
90 67
91 View* widget_container = new View(); 68 // If the Widget has no contents by default, add a view with a 'Close' button.
92 widget_container->SetLayoutManager(new CenterLayout); 69 if (!widget->GetContentsView()) {
93 widget_container->AddChildView(button_container); 70 View* contents = new View();
94 71 contents->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
95 widget->SetContentsView(widget_container); 72 contents->set_background(Background::CreateSolidBackground(SK_ColorGRAY));
96 73 BuildButton(contents, "Close", CLOSE_WIDGET);
97 if (!transparent) { 74 widget->SetContentsView(contents);
98 widget_container->set_background(
99 Background::CreateStandardPanelBackground());
100 } 75 }
101 76
102 // Show the widget.
103 widget->Show(); 77 widget->Show();
104 } 78 }
105 79
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) { 80 void WidgetExample::ButtonPressed(Button* sender, const ui::Event& event) {
146 switch (sender->tag()) { 81 switch (sender->tag()) {
147 case POPUP: 82 case POPUP:
148 CreatePopup(sender, false); 83 ShowWidget(sender, GetPopupParams());
149 break; 84 break;
150 case TRANSPARENT_POPUP: 85 case DIALOG:
151 CreatePopup(sender, true); 86 ShowWidget(sender, GetDialogParams());
152 break; 87 break;
153 #if defined(OS_LINUX)
154 case CHILD: 88 case CHILD:
155 CreateChild(sender, false); 89 ShowWidget(sender, GetChildParams());
156 break; 90 break;
157 case TRANSPARENT_CHILD:
158 CreateChild(sender, true);
159 break;
160 #endif
161 case CLOSE_WIDGET: 91 case CLOSE_WIDGET:
162 sender->GetWidget()->Close(); 92 sender->GetWidget()->Close();
163 break; 93 break;
164 } 94 }
165 } 95 }
166 96
167 } // namespace examples 97 } // namespace examples
168 } // namespace views 98 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698