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/controls/native/native_view_host_aura.h"
7 #include "base/logging.h"
8 #include "ui/aura/client/aura_constants.h"
9 #include "ui/aura/client/focus_client.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_delegate.h"
12 #include "ui/base/cursor/cursor.h"
13 #include "ui/base/hit_test.h"
14 #include "ui/views/controls/native/native_view_host.h"
15 #include "ui/views/view_constants_aura.h"
16 #include "ui/views/widget/widget.h"
20 class NativeViewHostAura::ClippingWindowDelegate
: public aura::WindowDelegate
{
22 ClippingWindowDelegate() : native_view_(NULL
) {}
23 virtual ~ClippingWindowDelegate() {}
25 void set_native_view(aura::Window
* native_view
) {
26 native_view_
= native_view
;
29 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{ return gfx::Size(); }
30 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{ return gfx::Size(); }
31 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
32 const gfx::Rect
& new_bounds
) OVERRIDE
{}
33 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
34 return gfx::kNullCursor
;
36 virtual int GetNonClientComponent(const gfx::Point
& point
) const OVERRIDE
{
39 virtual bool ShouldDescendIntoChildForEventHandling(
41 const gfx::Point
& location
) OVERRIDE
{ return true; }
42 virtual bool CanFocus() OVERRIDE
{
43 // Ask the hosted native view's delegate because directly calling
44 // aura::Window::CanFocus() will call back into this when checking whether
46 return native_view_
&& native_view_
->delegate()
47 ? native_view_
->delegate()->CanFocus()
50 virtual void OnCaptureLost() OVERRIDE
{}
51 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{}
52 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
{}
53 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
54 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{}
55 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{}
56 virtual bool HasHitTestMask() const OVERRIDE
{ return false; }
57 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
60 aura::Window
* native_view_
;
63 NativeViewHostAura::NativeViewHostAura(NativeViewHost
* host
)
65 clipping_window_delegate_(new ClippingWindowDelegate()),
66 clipping_window_(clipping_window_delegate_
.get()) {
67 // Set the type so descendant views (including popups) get positioned
69 clipping_window_
.SetType(ui::wm::WINDOW_TYPE_CONTROL
);
70 clipping_window_
.Init(aura::WINDOW_LAYER_NOT_DRAWN
);
71 clipping_window_
.set_owned_by_parent(false);
72 clipping_window_
.SetName("NativeViewHostAuraClip");
73 clipping_window_
.layer()->SetMasksToBounds(true);
74 clipping_window_
.SetProperty(views::kHostViewKey
, static_cast<View
*>(host_
));
77 NativeViewHostAura::~NativeViewHostAura() {
78 if (host_
->native_view()) {
79 host_
->native_view()->RemoveObserver(this);
80 host_
->native_view()->ClearProperty(views::kHostViewKey
);
81 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
82 clipping_window_
.ClearProperty(views::kHostViewKey
);
83 if (host_
->native_view()->parent() == &clipping_window_
)
84 clipping_window_
.RemoveChild(host_
->native_view());
88 ////////////////////////////////////////////////////////////////////////////////
89 // NativeViewHostAura, NativeViewHostWrapper implementation:
90 void NativeViewHostAura::AttachNativeView() {
91 clipping_window_delegate_
->set_native_view(host_
->native_view());
92 host_
->native_view()->AddObserver(this);
93 host_
->native_view()->SetProperty(views::kHostViewKey
,
94 static_cast<View
*>(host_
));
98 void NativeViewHostAura::NativeViewDetaching(bool destroyed
) {
99 clipping_window_delegate_
->set_native_view(NULL
);
100 RemoveClippingWindow();
102 host_
->native_view()->RemoveObserver(this);
103 host_
->native_view()->ClearProperty(views::kHostViewKey
);
104 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
105 host_
->native_view()->Hide();
106 if (host_
->native_view()->parent())
107 Widget::ReparentNativeView(host_
->native_view(), NULL
);
111 void NativeViewHostAura::AddedToWidget() {
112 if (!host_
->native_view())
116 if (host_
->IsDrawn())
117 host_
->native_view()->Show();
119 host_
->native_view()->Hide();
123 void NativeViewHostAura::RemovedFromWidget() {
124 if (host_
->native_view()) {
125 host_
->native_view()->Hide();
126 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
127 if (host_
->native_view()->parent())
128 host_
->native_view()->parent()->RemoveChild(host_
->native_view());
129 RemoveClippingWindow();
133 void NativeViewHostAura::InstallClip(int x
, int y
, int w
, int h
) {
135 new gfx::Rect(host_
->ConvertRectToWidget(gfx::Rect(x
, y
, w
, h
))));
138 bool NativeViewHostAura::HasInstalledClip() {
142 void NativeViewHostAura::UninstallClip() {
146 void NativeViewHostAura::ShowWidget(int x
, int y
, int w
, int h
) {
149 if (host_
->fast_resize()) {
150 gfx::Point
origin(x
, y
);
151 views::View::ConvertPointFromWidget(host_
, &origin
);
152 InstallClip(origin
.x(), origin
.y(), w
, h
);
153 width
= host_
->native_view()->bounds().width();
154 height
= host_
->native_view()->bounds().height();
156 clipping_window_
.SetBounds(clip_rect_
? *clip_rect_
157 : gfx::Rect(x
, y
, w
, h
));
159 gfx::Point clip_offset
= clipping_window_
.bounds().origin();
160 host_
->native_view()->SetBounds(
161 gfx::Rect(x
- clip_offset
.x(), y
- clip_offset
.y(), width
, height
));
162 host_
->native_view()->Show();
163 clipping_window_
.Show();
166 void NativeViewHostAura::HideWidget() {
167 host_
->native_view()->Hide();
170 void NativeViewHostAura::SetFocus() {
171 aura::Window
* window
= host_
->native_view();
172 aura::client::FocusClient
* client
= aura::client::GetFocusClient(window
);
174 client
->FocusWindow(window
);
177 gfx::NativeViewAccessible
NativeViewHostAura::GetNativeViewAccessible() {
181 gfx::NativeCursor
NativeViewHostAura::GetCursor(int x
, int y
) {
182 if (host_
->native_view())
183 return host_
->native_view()->GetCursor(gfx::Point(x
, y
));
184 return gfx::kNullCursor
;
187 void NativeViewHostAura::OnWindowDestroying(aura::Window
* window
) {
188 DCHECK(window
== host_
->native_view());
189 clipping_window_delegate_
->set_native_view(NULL
);
192 void NativeViewHostAura::OnWindowDestroyed(aura::Window
* window
) {
193 DCHECK(window
== host_
->native_view());
194 host_
->NativeViewDestroyed();
198 NativeViewHostWrapper
* NativeViewHostWrapper::CreateWrapper(
199 NativeViewHost
* host
) {
200 return new NativeViewHostAura(host
);
203 void NativeViewHostAura::AddClippingWindow() {
204 RemoveClippingWindow();
206 host_
->native_view()->SetProperty(aura::client::kHostWindowKey
,
207 host_
->GetWidget()->GetNativeView());
208 Widget::ReparentNativeView(host_
->native_view(),
210 if (host_
->GetWidget()->GetNativeView()) {
211 Widget::ReparentNativeView(&clipping_window_
,
212 host_
->GetWidget()->GetNativeView());
216 void NativeViewHostAura::RemoveClippingWindow() {
217 clipping_window_
.Hide();
218 if (host_
->native_view())
219 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
221 if (host_
->native_view()->parent() == &clipping_window_
) {
222 if (host_
->GetWidget() && host_
->GetWidget()->GetNativeView()) {
223 Widget::ReparentNativeView(host_
->native_view(),
224 host_
->GetWidget()->GetNativeView());
226 clipping_window_
.RemoveChild(host_
->native_view());
228 host_
->native_view()->SetBounds(clipping_window_
.bounds());
230 if (clipping_window_
.parent())
231 clipping_window_
.parent()->RemoveChild(&clipping_window_
);