Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / native / native_view_host.cc
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.h"
6
7 #include "base/logging.h"
8 #include "ui/base/cursor/cursor.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/accessibility/native_view_accessibility.h"
11 #include "ui/views/controls/native/native_view_host_wrapper.h"
12 #include "ui/views/widget/widget.h"
13
14 namespace views {
15
16 // static
17 const char NativeViewHost::kViewClassName[] = "NativeViewHost";
18 const char kWidgetNativeViewHostKey[] = "WidgetNativeViewHost";
19
20 ////////////////////////////////////////////////////////////////////////////////
21 // NativeViewHost, public:
22
23 NativeViewHost::NativeViewHost()
24     : native_view_(NULL),
25       fast_resize_(false),
26       fast_resize_at_last_layout_(false) {
27 }
28
29 NativeViewHost::~NativeViewHost() {
30 }
31
32 void NativeViewHost::Attach(gfx::NativeView native_view) {
33   DCHECK(native_view);
34   DCHECK(!native_view_);
35   native_view_ = native_view;
36   native_wrapper_->AttachNativeView();
37   Layout();
38
39   Widget* widget = Widget::GetWidgetForNativeView(native_view);
40   if (widget)
41     widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, this);
42 }
43
44 void NativeViewHost::Detach() {
45   Detach(false);
46 }
47
48 void NativeViewHost::SetPreferredSize(const gfx::Size& size) {
49   preferred_size_ = size;
50   PreferredSizeChanged();
51 }
52
53 void NativeViewHost::NativeViewDestroyed() {
54   // Detach so we can clear our state and notify the native_wrapper_ to release
55   // ref on the native view.
56   Detach(true);
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60 // NativeViewHost, View overrides:
61
62 gfx::Size NativeViewHost::GetPreferredSize() const {
63   return preferred_size_;
64 }
65
66 void NativeViewHost::Layout() {
67   if (!native_view_ || !native_wrapper_.get())
68     return;
69
70   gfx::Rect vis_bounds = GetVisibleBounds();
71   bool visible = !vis_bounds.IsEmpty();
72
73   if (visible && !fast_resize_) {
74     if (vis_bounds.size() != size()) {
75       // Only a portion of the Widget is really visible.
76       int x = vis_bounds.x();
77       int y = vis_bounds.y();
78       native_wrapper_->InstallClip(x, y, vis_bounds.width(),
79                                    vis_bounds.height());
80     } else if (native_wrapper_->HasInstalledClip()) {
81       // The whole widget is visible but we installed a clip on the widget,
82       // uninstall it.
83       native_wrapper_->UninstallClip();
84     }
85   }
86
87   if (visible) {
88     // Since widgets know nothing about the View hierarchy (they are direct
89     // children of the Widget that hosts our View hierarchy) they need to be
90     // positioned in the coordinate system of the Widget, not the current
91     // view.  Also, they should be positioned respecting the border insets
92     // of the native view.
93     gfx::Rect local_bounds = ConvertRectToWidget(GetContentsBounds());
94     native_wrapper_->ShowWidget(local_bounds.x(), local_bounds.y(),
95                                 local_bounds.width(),
96                                 local_bounds.height());
97   } else {
98     native_wrapper_->HideWidget();
99   }
100   fast_resize_at_last_layout_ = visible && fast_resize_;
101 }
102
103 void NativeViewHost::OnPaint(gfx::Canvas* canvas) {
104   // Paint background if there is one. NativeViewHost needs to paint
105   // a background when it is hosted in a TabbedPane. For Gtk implementation,
106   // NativeTabbedPaneGtk uses a NativeWidgetGtk as page container and because
107   // NativeWidgetGtk hook "expose" with its root view's paint, we need to
108   // fill the content. Otherwise, the tab page's background is not properly
109   // cleared. For Windows case, it appears okay to not paint background because
110   // we don't have a container window in-between. However if you want to use
111   // customized background, then this becomes necessary.
112   OnPaintBackground(canvas);
113
114   // The area behind our window is black, so during a fast resize (where our
115   // content doesn't draw over the full size of our native view, and the native
116   // view background color doesn't show up), we need to cover that blackness
117   // with something so that fast resizes don't result in black flash.
118   //
119   // It would be nice if this used some approximation of the page's
120   // current background color.
121   if (native_wrapper_->HasInstalledClip())
122     canvas->FillRect(GetLocalBounds(), SK_ColorWHITE);
123 }
124
125 void NativeViewHost::VisibilityChanged(View* starting_from, bool is_visible) {
126   Layout();
127 }
128
129 bool NativeViewHost::GetNeedsNotificationWhenVisibleBoundsChange() const {
130   // The native widget is placed relative to the root. As such, we need to
131   // know when the position of any ancestor changes, or our visibility relative
132   // to other views changed as it'll effect our position relative to the root.
133   return true;
134 }
135
136 void NativeViewHost::OnVisibleBoundsChanged() {
137   Layout();
138 }
139
140 void NativeViewHost::ViewHierarchyChanged(
141     const ViewHierarchyChangedDetails& details) {
142   views::Widget* this_widget = GetWidget();
143
144   // A non-NULL |details.move_view| indicates a move operation i.e. |this| is
145   // is being reparented.  If the previous and new parents belong to the same
146   // widget, don't remove |this| from the widget.  This saves resources from
147   // removing from widget and immediately followed by adding to widget; in
148   // particular, there wouldn't be spurious visibilitychange events for web
149   // contents of |WebView|.
150   if (details.move_view && this_widget &&
151       details.move_view->GetWidget() == this_widget) {
152     return;
153   }
154
155   if (details.is_add && this_widget) {
156     if (!native_wrapper_.get())
157       native_wrapper_.reset(NativeViewHostWrapper::CreateWrapper(this));
158     native_wrapper_->AddedToWidget();
159   } else if (!details.is_add) {
160     native_wrapper_->RemovedFromWidget();
161   }
162 }
163
164 const char* NativeViewHost::GetClassName() const {
165   return kViewClassName;
166 }
167
168 void NativeViewHost::OnFocus() {
169   native_wrapper_->SetFocus();
170   NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
171 }
172
173 gfx::NativeViewAccessible NativeViewHost::GetNativeViewAccessible() {
174   if (native_wrapper_.get()) {
175     gfx::NativeViewAccessible accessible_view =
176         native_wrapper_->GetNativeViewAccessible();
177     if (accessible_view)
178       return accessible_view;
179   }
180
181   return View::GetNativeViewAccessible();
182 }
183
184 gfx::NativeCursor NativeViewHost::GetCursor(const ui::MouseEvent& event) {
185   return native_wrapper_->GetCursor(event.x(), event.y());
186 }
187
188 ////////////////////////////////////////////////////////////////////////////////
189 // NativeViewHost, private:
190
191 void NativeViewHost::Detach(bool destroyed) {
192   if (native_view_) {
193     if (!destroyed) {
194       Widget* widget = Widget::GetWidgetForNativeView(native_view_);
195       if (widget)
196         widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, NULL);
197       ClearFocus();
198     }
199     native_wrapper_->NativeViewDetaching(destroyed);
200     native_view_ = NULL;
201   }
202 }
203
204 void NativeViewHost::ClearFocus() {
205   FocusManager* focus_manager = GetFocusManager();
206   if (!focus_manager || !focus_manager->GetFocusedView())
207     return;
208
209   Widget::Widgets widgets;
210   Widget::GetAllChildWidgets(native_view(), &widgets);
211   for (Widget::Widgets::iterator i = widgets.begin(); i != widgets.end(); ++i) {
212     focus_manager->ViewRemoved((*i)->GetRootView());
213     if (!focus_manager->GetFocusedView())
214       return;
215   }
216 }
217
218 }  // namespace views