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
3 // found in the LICENSE file.
5 #include "ui/views/test/child_modal_window.h"
7 #include "base/strings/utf_string_conversions.h" // ASCIIToUTF16
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/corewm/window_modality_controller.h"
15 #include "ui/views/widget/widget.h"
16 #include "ui/views/widget/widget_delegate.h"
23 // Parent window size and position.
24 const int kWindowLeft = 170;
25 const int kWindowTop = 200;
26 const int kWindowWidth = 400;
27 const int kWindowHeight = 400;
29 // Parent window layout.
30 const int kButtonHeight = 35;
31 const int kTextfieldHeight = 35;
34 const int kChildWindowWidth = 330;
35 const int kChildWindowHeight = 200;
37 // Child window layout.
38 const int kChildTextfieldLeft = 20;
39 const int kChildTextfieldTop = 50;
40 const int kChildTextfieldWidth = 290;
41 const int kChildTextfieldHeight = 35;
43 const SkColor kModalParentColor = SK_ColorWHITE;
44 const SkColor kChildColor = SK_ColorWHITE;
48 void CreateChildModalParent(gfx::NativeView context) {
49 Widget::CreateWindowWithContextAndBounds(
50 new ChildModalParent(context),
52 gfx::Rect(kWindowLeft, kWindowTop, kWindowWidth, kWindowHeight))->Show();
56 class ChildModalWindow : public WidgetDelegateView {
59 virtual ~ChildModalWindow();
62 // Overridden from View:
63 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
64 virtual gfx::Size GetPreferredSize() OVERRIDE;
66 // Overridden from WidgetDelegate:
67 virtual View* GetContentsView() OVERRIDE;
68 virtual string16 GetWindowTitle() const OVERRIDE;
69 virtual bool CanResize() const OVERRIDE;
70 virtual ui::ModalType GetModalType() const OVERRIDE;
72 DISALLOW_COPY_AND_ASSIGN(ChildModalWindow);
75 ChildModalWindow::ChildModalWindow() {
76 Textfield* textfield = new Textfield;
77 AddChildView(textfield);
79 kChildTextfieldLeft, kChildTextfieldTop,
80 kChildTextfieldWidth, kChildTextfieldHeight);
83 ChildModalWindow::~ChildModalWindow() {
86 void ChildModalWindow::OnPaint(gfx::Canvas* canvas) {
87 canvas->FillRect(GetLocalBounds(), kChildColor);
90 gfx::Size ChildModalWindow::GetPreferredSize() {
91 return gfx::Size(kChildWindowWidth, kChildWindowHeight);
94 View* ChildModalWindow::GetContentsView() {
98 string16 ChildModalWindow::GetWindowTitle() const {
99 return ASCIIToUTF16("Examples: Child Modal Window");
102 bool ChildModalWindow::CanResize() const {
106 ui::ModalType ChildModalWindow::GetModalType() const {
107 return ui::MODAL_TYPE_CHILD;
110 ChildModalParent::ChildModalParent(gfx::NativeView context)
111 : button_(new LabelButton(this,
112 ASCIIToUTF16("Show/Hide Child Modal Window"))),
113 textfield_(new Textfield),
114 host_(new NativeViewHost),
117 Widget* widget = new Widget;
118 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
119 params.context = context;
120 widget->Init(params);
121 widget->GetRootView()->set_background(
122 Background::CreateSolidBackground(kModalParentColor));
123 modal_parent_ = widget->GetNativeView();
124 widget->GetNativeView()->SetName("ModalParent");
125 AddChildView(button_);
126 AddChildView(textfield_);
130 ChildModalParent::~ChildModalParent() {
133 void ChildModalParent::ShowChild() {
135 child_ = CreateChild();
139 gfx::NativeWindow ChildModalParent::GetModalParent() const {
140 return modal_parent_;
143 gfx::NativeWindow ChildModalParent::GetChild() const {
145 return child_->GetNativeView();
149 Widget* ChildModalParent::CreateChild() {
150 Widget* child = Widget::CreateWindowWithParent(
151 new ChildModalWindow, GetWidget()->GetNativeView());
152 corewm::SetModalParent(child->GetNativeView(), GetModalParent());
153 child->AddObserver(this);
154 child->GetNativeView()->SetName("ChildModalWindow");
158 View* ChildModalParent::GetContentsView() {
162 string16 ChildModalParent::GetWindowTitle() const {
163 return ASCIIToUTF16("Examples: Child Modal Parent");
166 bool ChildModalParent::CanResize() const {
170 void ChildModalParent::DeleteDelegate() {
172 child_->RemoveObserver(this);
180 void ChildModalParent::Layout() {
182 button_->SetBounds(x(), running_y, width(), kButtonHeight);
183 running_y += kButtonHeight;
184 textfield_->SetBounds(x(), running_y, width(), kTextfieldHeight);
185 running_y += kTextfieldHeight;
186 host_->SetBounds(x(), running_y, width(), height() - running_y);
189 void ChildModalParent::ViewHierarchyChanged(
190 const ViewHierarchyChangedDetails& details) {
191 if (details.is_add && details.child == this) {
192 host_->Attach(modal_parent_);
193 GetWidget()->GetNativeView()->SetName("Parent");
197 void ChildModalParent::ButtonPressed(Button* sender,
198 const ui::Event& event) {
199 if (sender == button_) {
201 child_ = CreateChild();
202 if (child_->IsVisible())
209 void ChildModalParent::OnWidgetDestroying(Widget* widget) {
211 DCHECK_EQ(child_, widget);