OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/test/child_modal_window.h" | |
6 | |
7 #include "ash/wm/window_modality_controller.h" | |
8 #include "base/utf_string_conversions.h" // ASCIIToUTF16 | |
9 #include "ui/aura/window.h" | |
10 #include "ui/gfx/canvas.h" | |
11 #include "ui/views/background.h" | |
12 #include "ui/views/controls/button/text_button.h" | |
13 #include "ui/views/controls/native/native_view_host.h" | |
14 #include "ui/views/controls/textfield/textfield.h" | |
15 #include "ui/views/widget/widget.h" | |
16 #include "ui/views/widget/widget_delegate.h" | |
17 | |
18 namespace ash { | |
19 namespace test { | |
20 | |
21 namespace { | |
22 | |
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; | |
28 | |
29 // Parent window layout. | |
30 const int kButtonHeight = 35; | |
31 const int kTextfieldHeight = 35; | |
32 | |
33 // Child window size. | |
34 const int kChildWindowWidth = 330; | |
35 const int kChildWindowHeight = 200; | |
36 | |
37 // Child window layout. | |
38 const int kChildTextfieldLeft = 20; | |
39 const int kChildTextfieldTop = 50; | |
40 const int kChildTextfieldWidth = 290; | |
41 const int kChildTextfieldHeight = 35; | |
42 | |
43 const SkColor kModalParentColor = SK_ColorWHITE; | |
44 const SkColor kChildColor = SK_ColorWHITE; | |
45 | |
46 } // namespace | |
47 | |
48 void CreateChildModalParent() { | |
49 views::Widget::CreateWindowWithBounds( | |
50 new ChildModalParent, | |
51 gfx::Rect(kWindowLeft, kWindowTop, kWindowWidth, kWindowHeight))->Show(); | |
52 } | |
53 | |
54 | |
55 class ChildModalWindow : public views::WidgetDelegateView { | |
56 public: | |
57 ChildModalWindow(); | |
58 virtual ~ChildModalWindow(); | |
59 | |
60 private: | |
61 // Overridden from views::View: | |
62 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
63 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
64 | |
65 // Overridden from views::WidgetDelegate: | |
66 virtual views::View* GetContentsView() OVERRIDE; | |
67 virtual string16 GetWindowTitle() const OVERRIDE; | |
68 virtual bool CanResize() const OVERRIDE; | |
69 virtual ui::ModalType GetModalType() const OVERRIDE; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(ChildModalWindow); | |
72 }; | |
73 | |
74 ChildModalWindow::ChildModalWindow() { | |
75 views::Textfield* textfield = new views::Textfield; | |
76 AddChildView(textfield); | |
77 textfield->SetBounds( | |
78 kChildTextfieldLeft, kChildTextfieldTop, | |
79 kChildTextfieldWidth, kChildTextfieldHeight); | |
80 } | |
81 | |
82 ChildModalWindow::~ChildModalWindow() { | |
83 } | |
84 | |
85 void ChildModalWindow::OnPaint(gfx::Canvas* canvas) { | |
86 canvas->FillRect(GetLocalBounds(), kChildColor); | |
87 } | |
88 | |
89 gfx::Size ChildModalWindow::GetPreferredSize() { | |
90 return gfx::Size(kChildWindowWidth, kChildWindowHeight); | |
91 } | |
92 | |
93 views::View* ChildModalWindow::GetContentsView() { | |
94 return this; | |
95 } | |
96 | |
97 string16 ChildModalWindow::GetWindowTitle() const { | |
98 return ASCIIToUTF16("Examples: Child Modal Window"); | |
99 } | |
100 | |
101 bool ChildModalWindow::CanResize() const { | |
102 return false; | |
103 } | |
104 | |
105 ui::ModalType ChildModalWindow::GetModalType() const { | |
106 return ui::MODAL_TYPE_CHILD; | |
107 } | |
108 | |
109 ChildModalParent::ChildModalParent() | |
110 : ALLOW_THIS_IN_INITIALIZER_LIST(button_(new views::NativeTextButton( | |
111 this, ASCIIToUTF16("Show/Hide Child Modal Window")))), | |
112 textfield_(new views::Textfield), | |
113 host_(new views::NativeViewHost), | |
114 modal_parent_(NULL), | |
115 child_(NULL) { | |
116 views::Widget* widget = new views::Widget; | |
117 widget->Init( | |
118 views::Widget::InitParams(views::Widget::InitParams::TYPE_CONTROL)); | |
119 widget->GetRootView()->set_background( | |
120 views::Background::CreateSolidBackground(kModalParentColor)); | |
121 modal_parent_ = widget->GetNativeView(); | |
122 widget->GetNativeView()->SetName("ModalParent"); | |
123 AddChildView(button_); | |
124 AddChildView(textfield_); | |
125 AddChildView(host_); | |
126 } | |
127 | |
128 ChildModalParent::~ChildModalParent() { | |
129 } | |
130 | |
131 void ChildModalParent::ShowChild() { | |
132 if (!child_) | |
133 child_ = CreateChild(); | |
134 child_->Show(); | |
135 } | |
136 | |
137 gfx::NativeWindow ChildModalParent::GetModalParent() const { | |
138 return modal_parent_; | |
139 } | |
140 | |
141 gfx::NativeWindow ChildModalParent::GetChild() const { | |
142 if (child_) | |
143 return child_->GetNativeView(); | |
144 return NULL; | |
145 } | |
146 | |
147 views::Widget* ChildModalParent::CreateChild() { | |
148 views::Widget* child = views::Widget::CreateWindowWithParent( | |
149 new ChildModalWindow, GetWidget()->GetNativeView()); | |
150 ash::SetModalParent(child->GetNativeView(), GetModalParent()); | |
151 child->AddObserver(this); | |
152 child->GetNativeView()->SetName("ChildModalWindow"); | |
153 return child; | |
154 } | |
155 | |
156 views::View* ChildModalParent::GetContentsView() { | |
157 return this; | |
158 } | |
159 | |
160 string16 ChildModalParent::GetWindowTitle() const { | |
161 return ASCIIToUTF16("Examples: Child Modal Parent"); | |
162 } | |
163 | |
164 bool ChildModalParent::CanResize() const { | |
165 return false; | |
166 } | |
167 | |
168 void ChildModalParent::DeleteDelegate() { | |
169 if (child_) { | |
170 child_->RemoveObserver(this); | |
171 child_->Close(); | |
172 child_ = NULL; | |
173 } | |
174 } | |
175 | |
176 void ChildModalParent::Layout() { | |
177 int running_y = y(); | |
178 button_->SetBounds(x(), running_y, width(), kButtonHeight); | |
179 running_y += kButtonHeight; | |
180 textfield_->SetBounds(x(), running_y, width(), kTextfieldHeight); | |
181 running_y += kTextfieldHeight; | |
182 host_->SetBounds(x(), running_y, width(), height() - running_y); | |
183 } | |
184 | |
185 void ChildModalParent::ViewHierarchyChanged(bool is_add, | |
186 views::View* parent, | |
187 views::View* child) { | |
188 if (is_add && child == this) { | |
189 host_->Attach(modal_parent_); | |
190 GetWidget()->GetNativeView()->SetName("Parent"); | |
191 } | |
192 } | |
193 | |
194 void ChildModalParent::ButtonPressed(views::Button* sender, | |
195 const ui::Event& event) { | |
196 if (sender == button_) { | |
197 if (!child_) | |
198 child_ = CreateChild(); | |
199 if (child_->IsVisible()) | |
200 child_->Hide(); | |
201 else | |
202 child_->Show(); | |
203 } | |
204 } | |
205 | |
206 void ChildModalParent::OnWidgetClosing(views::Widget* widget) { | |
207 if (child_) { | |
208 DCHECK_EQ(child_, widget); | |
209 child_ = NULL; | |
210 } | |
211 } | |
212 | |
213 } // namespace test | |
214 } // namespace ash | |
OLD | NEW |