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.h"
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"
17 const char NativeViewHost::kViewClassName
[] = "NativeViewHost";
18 const char kWidgetNativeViewHostKey
[] = "WidgetNativeViewHost";
20 ////////////////////////////////////////////////////////////////////////////////
21 // NativeViewHost, public:
23 NativeViewHost::NativeViewHost()
26 fast_resize_at_last_layout_(false),
30 NativeViewHost::~NativeViewHost() {
33 void NativeViewHost::Attach(gfx::NativeView native_view
) {
35 DCHECK(!native_view_
);
36 native_view_
= native_view
;
37 // If set_focus_view() has not been invoked, this view is the one that should
38 // be seen as focused when the native view receives focus.
41 native_wrapper_
->NativeViewWillAttach();
42 Widget::ReparentNativeView(native_view_
, GetWidget()->GetNativeView());
45 Widget
* widget
= Widget::GetWidgetForNativeView(native_view
);
47 widget
->SetNativeWindowProperty(kWidgetNativeViewHostKey
, this);
50 void NativeViewHost::Detach() {
54 void NativeViewHost::SetPreferredSize(const gfx::Size
& size
) {
55 preferred_size_
= size
;
56 PreferredSizeChanged();
59 void NativeViewHost::NativeViewDestroyed() {
60 // Detach so we can clear our state and notify the native_wrapper_ to release
61 // ref on the native view.
65 ////////////////////////////////////////////////////////////////////////////////
66 // NativeViewHost, View overrides:
68 gfx::Size
NativeViewHost::GetPreferredSize() const {
69 return preferred_size_
;
72 void NativeViewHost::Layout() {
73 if (!native_view_
|| !native_wrapper_
.get())
76 gfx::Rect vis_bounds
= GetVisibleBounds();
77 bool visible
= !vis_bounds
.IsEmpty();
79 if (visible
&& !fast_resize_
) {
80 if (vis_bounds
.size() != size()) {
81 // Only a portion of the Widget is really visible.
82 int x
= vis_bounds
.x();
83 int y
= vis_bounds
.y();
84 native_wrapper_
->InstallClip(x
, y
, vis_bounds
.width(),
86 } else if (native_wrapper_
->HasInstalledClip()) {
87 // The whole widget is visible but we installed a clip on the widget,
89 native_wrapper_
->UninstallClip();
94 // Since widgets know nothing about the View hierarchy (they are direct
95 // children of the Widget that hosts our View hierarchy) they need to be
96 // positioned in the coordinate system of the Widget, not the current
97 // view. Also, they should be positioned respecting the border insets
98 // of the native view.
99 gfx::Rect local_bounds
= ConvertRectToWidget(GetContentsBounds());
100 native_wrapper_
->ShowWidget(local_bounds
.x(), local_bounds
.y(),
101 local_bounds
.width(),
102 local_bounds
.height());
104 native_wrapper_
->HideWidget();
106 fast_resize_at_last_layout_
= visible
&& fast_resize_
;
109 void NativeViewHost::OnPaint(gfx::Canvas
* canvas
) {
110 // Paint background if there is one. NativeViewHost needs to paint
111 // a background when it is hosted in a TabbedPane. For Gtk implementation,
112 // NativeTabbedPaneGtk uses a NativeWidgetGtk as page container and because
113 // NativeWidgetGtk hook "expose" with its root view's paint, we need to
114 // fill the content. Otherwise, the tab page's background is not properly
115 // cleared. For Windows case, it appears okay to not paint background because
116 // we don't have a container window in-between. However if you want to use
117 // customized background, then this becomes necessary.
118 OnPaintBackground(canvas
);
120 // The area behind our window is black, so during a fast resize (where our
121 // content doesn't draw over the full size of our native view, and the native
122 // view background color doesn't show up), we need to cover that blackness
123 // with something so that fast resizes don't result in black flash.
125 // It would be nice if this used some approximation of the page's
126 // current background color.
127 if (native_wrapper_
->HasInstalledClip())
128 canvas
->FillRect(GetLocalBounds(), SK_ColorWHITE
);
131 void NativeViewHost::VisibilityChanged(View
* starting_from
, bool is_visible
) {
135 bool NativeViewHost::NeedsNotificationWhenVisibleBoundsChange() const {
136 // The native widget is placed relative to the root. As such, we need to
137 // know when the position of any ancestor changes, or our visibility relative
138 // to other views changed as it'll effect our position relative to the root.
142 void NativeViewHost::OnVisibleBoundsChanged() {
146 void NativeViewHost::ViewHierarchyChanged(
147 const ViewHierarchyChangedDetails
& details
) {
148 views::Widget
* this_widget
= GetWidget();
150 // A non-NULL |details.move_view| indicates a move operation i.e. |this| is
151 // is being reparented. If the previous and new parents belong to the same
152 // widget, don't remove |this| from the widget. This saves resources from
153 // removing from widget and immediately followed by adding to widget; in
154 // particular, there wouldn't be spurious visibilitychange events for web
155 // contents of |WebView|.
156 if (details
.move_view
&& this_widget
&&
157 details
.move_view
->GetWidget() == this_widget
) {
161 if (details
.is_add
&& this_widget
) {
162 if (!native_wrapper_
.get())
163 native_wrapper_
.reset(NativeViewHostWrapper::CreateWrapper(this));
164 native_wrapper_
->AddedToWidget();
165 } else if (!details
.is_add
) {
166 native_wrapper_
->RemovedFromWidget();
170 const char* NativeViewHost::GetClassName() const {
171 return kViewClassName
;
174 void NativeViewHost::OnFocus() {
175 native_wrapper_
->SetFocus();
176 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
179 gfx::NativeViewAccessible
NativeViewHost::GetNativeViewAccessible() {
180 if (native_wrapper_
.get()) {
181 gfx::NativeViewAccessible accessible_view
=
182 native_wrapper_
->GetNativeViewAccessible();
184 return accessible_view
;
187 return View::GetNativeViewAccessible();
190 gfx::NativeCursor
NativeViewHost::GetCursor(const ui::MouseEvent
& event
) {
191 return native_wrapper_
->GetCursor(event
.x(), event
.y());
194 ////////////////////////////////////////////////////////////////////////////////
195 // NativeViewHost, private:
197 void NativeViewHost::Detach(bool destroyed
) {
200 Widget
* widget
= Widget::GetWidgetForNativeView(native_view_
);
202 widget
->SetNativeWindowProperty(kWidgetNativeViewHostKey
, NULL
);
205 native_wrapper_
->NativeViewDetaching(destroyed
);
210 void NativeViewHost::ClearFocus() {
211 FocusManager
* focus_manager
= GetFocusManager();
212 if (!focus_manager
|| !focus_manager
->GetFocusedView())
215 Widget::Widgets widgets
;
216 Widget::GetAllChildWidgets(native_view(), &widgets
);
217 for (Widget::Widgets::iterator i
= widgets
.begin(); i
!= widgets
.end(); ++i
) {
218 focus_manager
->ViewRemoved((*i
)->GetRootView());
219 if (!focus_manager
->GetFocusedView())