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) {
29 NativeViewHost::~NativeViewHost() {
32 void NativeViewHost::Attach(gfx::NativeView native_view) {
34 DCHECK(!native_view_);
35 native_view_ = native_view;
36 native_wrapper_->AttachNativeView();
39 Widget* widget = Widget::GetWidgetForNativeView(native_view);
41 widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, this);
44 void NativeViewHost::Detach() {
48 void NativeViewHost::SetPreferredSize(const gfx::Size& size) {
49 preferred_size_ = size;
50 PreferredSizeChanged();
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.
59 ////////////////////////////////////////////////////////////////////////////////
60 // NativeViewHost, View overrides:
62 gfx::Size NativeViewHost::GetPreferredSize() const {
63 return preferred_size_;
66 void NativeViewHost::Layout() {
67 if (!native_view_ || !native_wrapper_.get())
70 gfx::Rect vis_bounds = GetVisibleBounds();
71 bool visible = !vis_bounds.IsEmpty();
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(),
80 } else if (native_wrapper_->HasInstalledClip()) {
81 // The whole widget is visible but we installed a clip on the widget,
83 native_wrapper_->UninstallClip();
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(),
96 local_bounds.height());
98 native_wrapper_->HideWidget();
100 fast_resize_at_last_layout_ = visible && fast_resize_;
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);
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.
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);
125 void NativeViewHost::VisibilityChanged(View* starting_from, bool is_visible) {
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.
136 void NativeViewHost::OnVisibleBoundsChanged() {
140 void NativeViewHost::ViewHierarchyChanged(
141 const ViewHierarchyChangedDetails& details) {
142 views::Widget* this_widget = GetWidget();
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) {
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();
164 const char* NativeViewHost::GetClassName() const {
165 return kViewClassName;
168 void NativeViewHost::OnFocus() {
169 native_wrapper_->SetFocus();
170 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
173 gfx::NativeViewAccessible NativeViewHost::GetNativeViewAccessible() {
174 if (native_wrapper_.get()) {
175 gfx::NativeViewAccessible accessible_view =
176 native_wrapper_->GetNativeViewAccessible();
178 return accessible_view;
181 return View::GetNativeViewAccessible();
184 gfx::NativeCursor NativeViewHost::GetCursor(const ui::MouseEvent& event) {
185 return native_wrapper_->GetCursor(event.x(), event.y());
188 ////////////////////////////////////////////////////////////////////////////////
189 // NativeViewHost, private:
191 void NativeViewHost::Detach(bool destroyed) {
194 Widget* widget = Widget::GetWidgetForNativeView(native_view_);
196 widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, NULL);
199 native_wrapper_->NativeViewDetaching(destroyed);
204 void NativeViewHost::ClearFocus() {
205 FocusManager* focus_manager = GetFocusManager();
206 if (!focus_manager || !focus_manager->GetFocusedView())
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())