1 // Copyright 2014 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.
5 #include "ash/test/child_modal_window.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/aura/window.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/background.h"
11 #include "ui/views/controls/button/label_button.h"
12 #include "ui/views/controls/native/native_view_host.h"
13 #include "ui/views/controls/textfield/textfield.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_delegate.h"
16 #include "ui/wm/core/window_modality_controller.h"
25 // Parent window size and position.
26 const int kWindowLeft
= 170;
27 const int kWindowTop
= 200;
28 const int kWindowWidth
= 400;
29 const int kWindowHeight
= 400;
31 // Parent window layout.
32 const int kButtonHeight
= 35;
33 const int kTextfieldHeight
= 35;
36 const int kChildWindowWidth
= 330;
37 const int kChildWindowHeight
= 200;
39 // Child window layout.
40 const int kChildTextfieldLeft
= 20;
41 const int kChildTextfieldTop
= 50;
42 const int kChildTextfieldWidth
= 290;
43 const int kChildTextfieldHeight
= 35;
45 const SkColor kModalParentColor
= SK_ColorWHITE
;
46 const SkColor kChildColor
= SK_ColorWHITE
;
50 void CreateChildModalParent(gfx::NativeView context
) {
51 Widget::CreateWindowWithContextAndBounds(
52 new ChildModalParent(context
),
54 gfx::Rect(kWindowLeft
, kWindowTop
, kWindowWidth
, kWindowHeight
))->Show();
58 class ChildModalWindow
: public views::WidgetDelegateView
{
61 virtual ~ChildModalWindow();
64 // Overridden from View:
65 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
66 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
68 // Overridden from WidgetDelegate:
69 virtual View
* GetContentsView() OVERRIDE
;
70 virtual base::string16
GetWindowTitle() const OVERRIDE
;
71 virtual bool CanResize() const OVERRIDE
;
72 virtual ui::ModalType
GetModalType() const OVERRIDE
;
74 DISALLOW_COPY_AND_ASSIGN(ChildModalWindow
);
77 ChildModalWindow::ChildModalWindow() {
78 views::Textfield
* textfield
= new views::Textfield
;
79 AddChildView(textfield
);
81 kChildTextfieldLeft
, kChildTextfieldTop
,
82 kChildTextfieldWidth
, kChildTextfieldHeight
);
85 ChildModalWindow::~ChildModalWindow() {
88 void ChildModalWindow::OnPaint(gfx::Canvas
* canvas
) {
89 canvas
->FillRect(GetLocalBounds(), kChildColor
);
92 gfx::Size
ChildModalWindow::GetPreferredSize() const {
93 return gfx::Size(kChildWindowWidth
, kChildWindowHeight
);
96 views::View
* ChildModalWindow::GetContentsView() {
100 base::string16
ChildModalWindow::GetWindowTitle() const {
101 return base::ASCIIToUTF16("Examples: Child Modal Window");
104 bool ChildModalWindow::CanResize() const {
108 ui::ModalType
ChildModalWindow::GetModalType() const {
109 return ui::MODAL_TYPE_CHILD
;
112 ChildModalParent::ChildModalParent(gfx::NativeView context
)
113 : button_(new views::LabelButton(this,
115 "Show/Hide Child Modal Window"))),
116 textfield_(new views::Textfield
),
117 host_(new views::NativeViewHost
),
120 Widget
* widget
= new Widget
;
121 Widget::InitParams
params(Widget::InitParams::TYPE_CONTROL
);
122 params
.context
= context
;
123 widget
->Init(params
);
124 widget
->GetRootView()->set_background(
125 views::Background::CreateSolidBackground(kModalParentColor
));
126 modal_parent_
= widget
->GetNativeView();
127 widget
->GetNativeView()->SetName("ModalParent");
128 AddChildView(button_
);
129 AddChildView(textfield_
);
133 ChildModalParent::~ChildModalParent() {
136 void ChildModalParent::ShowChild() {
138 child_
= CreateChild();
142 gfx::NativeWindow
ChildModalParent::GetModalParent() const {
143 return modal_parent_
;
146 gfx::NativeWindow
ChildModalParent::GetChild() const {
148 return child_
->GetNativeView();
152 Widget
* ChildModalParent::CreateChild() {
153 Widget
* child
= Widget::CreateWindowWithParent(
154 new ChildModalWindow
, GetWidget()->GetNativeView());
155 wm::SetModalParent(child
->GetNativeView(), GetModalParent());
156 child
->AddObserver(this);
157 child
->GetNativeView()->SetName("ChildModalWindow");
161 views::View
* ChildModalParent::GetContentsView() {
165 base::string16
ChildModalParent::GetWindowTitle() const {
166 return base::ASCIIToUTF16("Examples: Child Modal Parent");
169 bool ChildModalParent::CanResize() const {
173 void ChildModalParent::DeleteDelegate() {
175 child_
->RemoveObserver(this);
183 void ChildModalParent::Layout() {
185 button_
->SetBounds(x(), running_y
, width(), kButtonHeight
);
186 running_y
+= kButtonHeight
;
187 textfield_
->SetBounds(x(), running_y
, width(), kTextfieldHeight
);
188 running_y
+= kTextfieldHeight
;
189 host_
->SetBounds(x(), running_y
, width(), height() - running_y
);
192 void ChildModalParent::ViewHierarchyChanged(
193 const ViewHierarchyChangedDetails
& details
) {
194 if (details
.is_add
&& details
.child
== this) {
195 host_
->Attach(modal_parent_
);
196 GetWidget()->GetNativeView()->SetName("Parent");
200 void ChildModalParent::ButtonPressed(views::Button
* sender
,
201 const ui::Event
& event
) {
202 if (sender
== button_
) {
204 child_
= CreateChild();
205 if (child_
->IsVisible())
212 void ChildModalParent::OnWidgetDestroying(Widget
* widget
) {
214 DCHECK_EQ(child_
, widget
);