OLD | NEW |
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/controls/native/native_view_host_aura.h" | 5 #include "ui/views/controls/native/native_view_host_aura.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/aura/client/focus_client.h" | 8 #include "ui/aura/client/focus_client.h" |
9 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
| 10 #include "ui/gfx/safe_integer_conversions.h" |
10 #include "ui/views/controls/native/native_view_host.h" | 11 #include "ui/views/controls/native/native_view_host.h" |
11 #include "ui/views/view_constants_aura.h" | 12 #include "ui/views/view_constants_aura.h" |
12 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
13 | 14 |
14 namespace views { | 15 namespace views { |
15 | 16 |
16 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) | 17 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) |
17 : host_(host), | 18 : host_(host), |
18 installed_clip_(false) { | 19 installed_clip_(false), |
| 20 clipping_window_(NULL) { |
19 } | 21 } |
20 | 22 |
21 NativeViewHostAura::~NativeViewHostAura() { | 23 NativeViewHostAura::~NativeViewHostAura() { |
22 if (host_->native_view()) { | 24 if (clipping_window_) |
23 host_->native_view()->ClearProperty(views::kHostViewKey); | 25 delete clipping_window_; |
| 26 |
| 27 if (host_->native_view()) |
24 host_->native_view()->RemoveObserver(this); | 28 host_->native_view()->RemoveObserver(this); |
25 } | |
26 } | 29 } |
27 | 30 |
28 //////////////////////////////////////////////////////////////////////////////// | 31 //////////////////////////////////////////////////////////////////////////////// |
29 // NativeViewHostAura, NativeViewHostWrapper implementation: | 32 // NativeViewHostAura, NativeViewHostWrapper implementation: |
30 void NativeViewHostAura::NativeViewWillAttach() { | 33 void NativeViewHostAura::AttachNativeView() { |
31 host_->native_view()->AddObserver(this); | 34 host_->native_view()->AddObserver(this); |
32 host_->native_view()->SetProperty(views::kHostViewKey, | 35 AddClippingWindow(); |
33 static_cast<View*>(host_)); | |
34 } | 36 } |
35 | 37 |
36 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { | 38 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { |
37 if (!destroyed) { | 39 if (!destroyed) { |
38 host_->native_view()->ClearProperty(views::kHostViewKey); | |
39 host_->native_view()->RemoveObserver(this); | 40 host_->native_view()->RemoveObserver(this); |
40 host_->native_view()->Hide(); | 41 host_->native_view()->Hide(); |
41 if (host_->native_view()->parent()) | 42 RemoveClippingWindow(); |
42 Widget::ReparentNativeView(host_->native_view(), NULL); | 43 } else { |
| 44 if (clipping_window_) { |
| 45 delete clipping_window_; |
| 46 clipping_window_ = NULL; |
| 47 } |
43 } | 48 } |
44 } | 49 } |
45 | 50 |
46 void NativeViewHostAura::AddedToWidget() { | 51 void NativeViewHostAura::AddedToWidget() { |
47 if (!host_->native_view()) | 52 if (!host_->native_view()) |
48 return; | 53 return; |
49 | 54 AddClippingWindow(); |
50 aura::Window* widget_window = host_->GetWidget()->GetNativeView(); | |
51 if (host_->native_view()->parent() != widget_window) | |
52 widget_window->AddChild(host_->native_view()); | |
53 if (host_->IsDrawn()) | 55 if (host_->IsDrawn()) |
54 host_->native_view()->Show(); | 56 host_->native_view()->Show(); |
55 else | 57 else |
56 host_->native_view()->Hide(); | 58 host_->native_view()->Hide(); |
57 host_->Layout(); | 59 host_->Layout(); |
58 } | 60 } |
59 | 61 |
60 void NativeViewHostAura::RemovedFromWidget() { | 62 void NativeViewHostAura::RemovedFromWidget() { |
61 if (host_->native_view()) { | 63 if (host_->native_view()) { |
62 host_->native_view()->Hide(); | 64 host_->native_view()->Hide(); |
63 if (host_->native_view()->parent()) | |
64 host_->native_view()->parent()->RemoveChild(host_->native_view()); | |
65 } | 65 } |
| 66 RemoveClippingWindow(); |
66 } | 67 } |
67 | 68 |
68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { | 69 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { |
69 // Note that this does not pose a problem functionality wise - it might | 70 installed_clip_ = true; |
70 // however pose a speed degradation if not implemented. | 71 clip_rect_ = gfx::Rect(x + orig_bounds_.x(), |
71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 72 y + orig_bounds_.y(), |
| 73 w, |
| 74 h); |
| 75 UpdateClippingWindow(); |
| 76 clipping_window_->layer()->SetMasksToBounds(true); |
72 } | 77 } |
73 | 78 |
74 bool NativeViewHostAura::HasInstalledClip() { | 79 bool NativeViewHostAura::HasInstalledClip() { |
75 return installed_clip_; | 80 return installed_clip_; |
76 } | 81 } |
77 | 82 |
78 void NativeViewHostAura::UninstallClip() { | 83 void NativeViewHostAura::UninstallClip() { |
| 84 if (installed_clip_ == false) |
| 85 return; |
79 installed_clip_ = false; | 86 installed_clip_ = false; |
| 87 clipping_window_->layer()->SetMasksToBounds(false); |
80 } | 88 } |
81 | 89 |
82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 90 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
83 // TODO: need to support fast resize. | 91 if (host_->fast_resize()) { |
84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 92 gfx::Rect native_view_bounds = host_->native_view()->bounds(); |
| 93 gfx::Point native_view_origin = |
| 94 CalculateNativeViewOrigin(clipping_window_->bounds(), |
| 95 native_view_bounds); |
| 96 orig_bounds_ = gfx::Rect(x + native_view_origin.x(), |
| 97 y + native_view_origin.y(), |
| 98 native_view_bounds.width(), |
| 99 native_view_bounds.height()); |
| 100 |
| 101 InstallClip(x - orig_bounds_.x(), |
| 102 y - orig_bounds_.y(), |
| 103 w, |
| 104 h); |
| 105 } else { |
| 106 clip_rect_.Offset(x - orig_bounds_.x(), y - orig_bounds_.y()); |
| 107 orig_bounds_ = gfx::Rect(x, y, w, h); |
| 108 UpdateClippingWindow(); |
| 109 } |
85 host_->native_view()->Show(); | 110 host_->native_view()->Show(); |
86 } | 111 } |
87 | 112 |
88 void NativeViewHostAura::HideWidget() { | 113 void NativeViewHostAura::HideWidget() { |
89 host_->native_view()->Hide(); | 114 host_->native_view()->Hide(); |
90 } | 115 } |
91 | 116 |
92 void NativeViewHostAura::SetFocus() { | 117 void NativeViewHostAura::SetFocus() { |
93 aura::Window* window = host_->native_view(); | 118 aura::Window* window = host_->native_view(); |
94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 119 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
95 if (client) | 120 if (client) |
96 client->FocusWindow(window); | 121 client->FocusWindow(window); |
97 } | 122 } |
98 | 123 |
99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 124 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
100 return NULL; | 125 return NULL; |
101 } | 126 } |
102 | 127 |
103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 128 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
104 DCHECK(window == host_->native_view()); | 129 DCHECK(window == host_->native_view()); |
105 host_->NativeViewDestroyed(); | 130 host_->NativeViewDestroyed(); |
106 } | 131 } |
107 | 132 |
108 // static | 133 // static |
109 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 134 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
110 NativeViewHost* host) { | 135 NativeViewHost* host) { |
111 return new NativeViewHostAura(host); | 136 return new NativeViewHostAura(host); |
112 } | 137 } |
113 | 138 |
| 139 gfx::Point NativeViewHostAura::CalculateNativeViewOrigin( |
| 140 const gfx::Rect& input_rect, |
| 141 const gfx::Rect& native_rect) const { |
| 142 int new_x = gfx::ToRoundedInt(host_->GetWidthScaleFactor() * |
| 143 (input_rect.width() - |
| 144 native_rect.width())); |
| 145 int new_y = gfx::ToRoundedInt(host_->GetHeightScaleFactor() * |
| 146 (input_rect.height() - |
| 147 native_rect.height())); |
| 148 return gfx::Point(new_x, new_y); |
| 149 } |
| 150 |
| 151 void NativeViewHostAura::AddClippingWindow() { |
| 152 RemoveClippingWindow(); |
| 153 clipping_window_ = new aura::Window(NULL); |
| 154 clipping_window_->set_owned_by_parent(false); |
| 155 clipping_window_->SetTransparent(true); |
| 156 clipping_window_->Init(ui::LAYER_NOT_DRAWN); |
| 157 clipping_window_->SetName("NativeViewHostAuraClip"); |
| 158 clipping_window_->layer()->SetMasksToBounds(false); |
| 159 |
| 160 gfx::Rect bounds = host_->native_view()->bounds(); |
| 161 orig_bounds_ = bounds; |
| 162 clipping_window_->SetBounds(bounds); |
| 163 bounds.set_origin(gfx::Point(0, 0)); |
| 164 host_->native_view()->SetBounds(bounds); |
| 165 |
| 166 clipping_window_->AddChild(host_->native_view()); |
| 167 clipping_window_->SetProperty(views::kHostViewKey, |
| 168 static_cast<View*>(host_)); |
| 169 Widget::ReparentNativeView(clipping_window_, |
| 170 host_->GetWidget()->GetNativeView()); |
| 171 clipping_window_->Show(); |
| 172 host_->native_view()->SetName("ClippedContent"); |
| 173 } |
| 174 |
| 175 void NativeViewHostAura::RemoveClippingWindow() { |
| 176 if (clipping_window_) { |
| 177 if (host_->native_view() && |
| 178 host_->native_view()->parent() == clipping_window_) { |
| 179 host_->native_view()->SetBounds(clipping_window_->bounds()); |
| 180 if (clipping_window_ && clipping_window_->parent()) { |
| 181 Widget::ReparentNativeView(host_->native_view(), NULL); |
| 182 } else { |
| 183 clipping_window_->RemoveChild(host_->native_view()); |
| 184 } |
| 185 } |
| 186 delete clipping_window_; |
| 187 clipping_window_ = NULL; |
| 188 } |
| 189 } |
| 190 |
| 191 void NativeViewHostAura::UpdateClippingWindow() { |
| 192 if (!installed_clip_) |
| 193 clip_rect_ = orig_bounds_; |
| 194 |
| 195 clipping_window_->SetBounds(clip_rect_); |
| 196 |
| 197 gfx::Rect native_view_bounds = orig_bounds_; |
| 198 native_view_bounds.Offset(-clip_rect_.x(), -clip_rect_.y()); |
| 199 host_->native_view()->SetBounds(native_view_bounds); |
| 200 } |
| 201 |
114 } // namespace views | 202 } // namespace views |
OLD | NEW |