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 "ui/views/controls/native/native_view_host_aura.h" 6 7 #include "base/logging.h" 8 #include "ui/aura/focus_manager.h" 9 #include "ui/aura/window.h" 10 #include "ui/views/controls/native/native_view_host.h" 11 #include "ui/views/view_constants_aura.h" 12 #include "ui/views/widget/widget.h" 13 14 namespace views { 15 16 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) 17 : host_(host), 18 installed_clip_(false) { 19 } 20 21 NativeViewHostAura::~NativeViewHostAura() { 22 if (host_->native_view()) { 23 host_->native_view()->ClearProperty(views::kHostViewKey); 24 host_->native_view()->RemoveObserver(this); 25 } 26 } 27 28 //////////////////////////////////////////////////////////////////////////////// 29 // NativeViewHostAura, NativeViewHostWrapper implementation: 30 void NativeViewHostAura::NativeViewWillAttach() { 31 host_->native_view()->AddObserver(this); 32 host_->native_view()->SetProperty(views::kHostViewKey, 33 static_cast<View*>(host_)); 34 } 35 36 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { 37 if (!destroyed) { 38 host_->native_view()->ClearProperty(views::kHostViewKey); 39 host_->native_view()->RemoveObserver(this); 40 host_->native_view()->Hide(); 41 if (host_->native_view()->parent()) 42 Widget::ReparentNativeView(host_->native_view(), NULL); 43 } 44 } 45 46 void NativeViewHostAura::AddedToWidget() { 47 if (!host_->native_view()) 48 return; 49 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()) 54 host_->native_view()->Show(); 55 else 56 host_->native_view()->Hide(); 57 host_->Layout(); 58 } 59 60 void NativeViewHostAura::RemovedFromWidget() { 61 if (host_->native_view()) { 62 host_->native_view()->Hide(); 63 if (host_->native_view()->parent()) 64 host_->native_view()->parent()->RemoveChild(host_->native_view()); 65 } 66 } 67 68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { 69 NOTIMPLEMENTED(); 70 } 71 72 bool NativeViewHostAura::HasInstalledClip() { 73 return installed_clip_; 74 } 75 76 void NativeViewHostAura::UninstallClip() { 77 installed_clip_ = false; 78 } 79 80 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { 81 // TODO: need to support fast resize. 82 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); 83 host_->native_view()->Show(); 84 } 85 86 void NativeViewHostAura::HideWidget() { 87 host_->native_view()->Hide(); 88 } 89 90 void NativeViewHostAura::SetFocus() { 91 aura::Window* window = host_->native_view(); 92 aura::client::FocusClient* client = aura::client::GetFocusClient(window); 93 if (client) 94 client->FocusWindow(window); 95 } 96 97 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { 98 return NULL; 99 } 100 101 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { 102 DCHECK(window == host_->native_view()); 103 host_->NativeViewDestroyed(); 104 } 105 106 // static 107 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( 108 NativeViewHost* host) { 109 return new NativeViewHostAura(host); 110 } 111 112 } // namespace views 113