Home | History | Annotate | Download | only in native
      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/client/focus_client.h"
      9 #include "ui/aura/window.h"
     10 #include "ui/base/cursor/cursor.h"
     11 #include "ui/views/controls/native/native_view_host.h"
     12 #include "ui/views/view_constants_aura.h"
     13 #include "ui/views/widget/widget.h"
     14 
     15 namespace views {
     16 
     17 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host)
     18     : host_(host),
     19       installed_clip_(false) {
     20 }
     21 
     22 NativeViewHostAura::~NativeViewHostAura() {
     23   if (host_->native_view()) {
     24     host_->native_view()->ClearProperty(views::kHostViewKey);
     25     host_->native_view()->RemoveObserver(this);
     26   }
     27 }
     28 
     29 ////////////////////////////////////////////////////////////////////////////////
     30 // NativeViewHostAura, NativeViewHostWrapper implementation:
     31 void NativeViewHostAura::NativeViewWillAttach() {
     32   host_->native_view()->AddObserver(this);
     33   host_->native_view()->SetProperty(views::kHostViewKey,
     34       static_cast<View*>(host_));
     35 }
     36 
     37 void NativeViewHostAura::NativeViewDetaching(bool destroyed) {
     38   if (!destroyed) {
     39     host_->native_view()->ClearProperty(views::kHostViewKey);
     40     host_->native_view()->RemoveObserver(this);
     41     host_->native_view()->Hide();
     42     if (host_->native_view()->parent())
     43       Widget::ReparentNativeView(host_->native_view(), NULL);
     44   }
     45 }
     46 
     47 void NativeViewHostAura::AddedToWidget() {
     48   if (!host_->native_view())
     49     return;
     50 
     51   aura::Window* widget_window = host_->GetWidget()->GetNativeView();
     52   if (host_->native_view()->parent() != widget_window)
     53     widget_window->AddChild(host_->native_view());
     54   if (host_->IsDrawn())
     55     host_->native_view()->Show();
     56   else
     57     host_->native_view()->Hide();
     58   host_->Layout();
     59 }
     60 
     61 void NativeViewHostAura::RemovedFromWidget() {
     62   if (host_->native_view()) {
     63     host_->native_view()->Hide();
     64     if (host_->native_view()->parent())
     65       host_->native_view()->parent()->RemoveChild(host_->native_view());
     66   }
     67 }
     68 
     69 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) {
     70   // Note that this does not pose a problem functionality wise - it might
     71   // however pose a speed degradation if not implemented.
     72   LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet.";
     73 }
     74 
     75 bool NativeViewHostAura::HasInstalledClip() {
     76   return installed_clip_;
     77 }
     78 
     79 void NativeViewHostAura::UninstallClip() {
     80   installed_clip_ = false;
     81 }
     82 
     83 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) {
     84   // TODO: need to support fast resize.
     85   host_->native_view()->SetBounds(gfx::Rect(x, y, w, h));
     86   host_->native_view()->Show();
     87 }
     88 
     89 void NativeViewHostAura::HideWidget() {
     90   host_->native_view()->Hide();
     91 }
     92 
     93 void NativeViewHostAura::SetFocus() {
     94   aura::Window* window = host_->native_view();
     95   aura::client::FocusClient* client = aura::client::GetFocusClient(window);
     96   if (client)
     97     client->FocusWindow(window);
     98 }
     99 
    100 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() {
    101   return NULL;
    102 }
    103 
    104 gfx::NativeCursor NativeViewHostAura::GetCursor(int x, int y) {
    105   if (host_->native_view())
    106     return host_->native_view()->GetCursor(gfx::Point(x, y));
    107   return gfx::kNullCursor;
    108 }
    109 
    110 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) {
    111   DCHECK(window == host_->native_view());
    112   host_->NativeViewDestroyed();
    113 }
    114 
    115 // static
    116 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper(
    117     NativeViewHost* host) {
    118   return new NativeViewHostAura(host);
    119 }
    120 
    121 }  // namespace views
    122