This source file includes following definitions.
- focus_view_
- Attach
- Detach
- SetPreferredSize
- NativeViewDestroyed
- GetPreferredSize
- Layout
- OnPaint
- VisibilityChanged
- NeedsNotificationWhenVisibleBoundsChange
- OnVisibleBoundsChanged
- ViewHierarchyChanged
- GetClassName
- OnFocus
- GetNativeViewAccessible
- GetCursor
- Detach
- ClearFocus
#include "ui/views/controls/native/native_view_host.h"
#include "base/logging.h"
#include "ui/base/cursor/cursor.h"
#include "ui/gfx/canvas.h"
#include "ui/views/accessibility/native_view_accessibility.h"
#include "ui/views/controls/native/native_view_host_wrapper.h"
#include "ui/views/widget/widget.h"
namespace views {
const char NativeViewHost::kViewClassName[] = "NativeViewHost";
const char kWidgetNativeViewHostKey[] = "WidgetNativeViewHost";
const bool NativeViewHost::kRenderNativeControlFocus = false;
NativeViewHost::NativeViewHost()
: native_view_(NULL),
fast_resize_(false),
fast_resize_at_last_layout_(false),
focus_view_(NULL) {
}
NativeViewHost::~NativeViewHost() {
}
void NativeViewHost::Attach(gfx::NativeView native_view) {
DCHECK(native_view);
DCHECK(!native_view_);
native_view_ = native_view;
if (!focus_view_)
focus_view_ = this;
native_wrapper_->NativeViewWillAttach();
Widget::ReparentNativeView(native_view_, GetWidget()->GetNativeView());
Layout();
Widget* widget = Widget::GetWidgetForNativeView(native_view);
if (widget)
widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, this);
}
void NativeViewHost::Detach() {
Detach(false);
}
void NativeViewHost::SetPreferredSize(const gfx::Size& size) {
preferred_size_ = size;
PreferredSizeChanged();
}
void NativeViewHost::NativeViewDestroyed() {
Detach(true);
}
gfx::Size NativeViewHost::GetPreferredSize() {
return preferred_size_;
}
void NativeViewHost::Layout() {
if (!native_view_ || !native_wrapper_.get())
return;
gfx::Rect vis_bounds = GetVisibleBounds();
bool visible = !vis_bounds.IsEmpty();
if (visible && !fast_resize_) {
if (vis_bounds.size() != size()) {
int x = vis_bounds.x();
int y = vis_bounds.y();
native_wrapper_->InstallClip(x, y, vis_bounds.width(),
vis_bounds.height());
} else if (native_wrapper_->HasInstalledClip()) {
native_wrapper_->UninstallClip();
}
}
if (visible) {
gfx::Rect local_bounds = ConvertRectToWidget(GetContentsBounds());
native_wrapper_->ShowWidget(local_bounds.x(), local_bounds.y(),
local_bounds.width(),
local_bounds.height());
} else {
native_wrapper_->HideWidget();
}
fast_resize_at_last_layout_ = visible && fast_resize_;
}
void NativeViewHost::OnPaint(gfx::Canvas* canvas) {
OnPaintBackground(canvas);
if (native_wrapper_->HasInstalledClip())
canvas->FillRect(GetLocalBounds(), SK_ColorWHITE);
}
void NativeViewHost::VisibilityChanged(View* starting_from, bool is_visible) {
Layout();
}
bool NativeViewHost::NeedsNotificationWhenVisibleBoundsChange() const {
return true;
}
void NativeViewHost::OnVisibleBoundsChanged() {
Layout();
}
void NativeViewHost::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
views::Widget* this_widget = GetWidget();
if (details.move_view && this_widget &&
details.move_view->GetWidget() == this_widget) {
return;
}
if (details.is_add && this_widget) {
if (!native_wrapper_.get())
native_wrapper_.reset(NativeViewHostWrapper::CreateWrapper(this));
native_wrapper_->AddedToWidget();
} else if (!details.is_add) {
native_wrapper_->RemovedFromWidget();
}
}
const char* NativeViewHost::GetClassName() const {
return kViewClassName;
}
void NativeViewHost::OnFocus() {
native_wrapper_->SetFocus();
NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
}
gfx::NativeViewAccessible NativeViewHost::GetNativeViewAccessible() {
if (native_wrapper_.get()) {
gfx::NativeViewAccessible accessible_view =
native_wrapper_->GetNativeViewAccessible();
if (accessible_view)
return accessible_view;
}
return View::GetNativeViewAccessible();
}
gfx::NativeCursor NativeViewHost::GetCursor(const ui::MouseEvent& event) {
return native_wrapper_->GetCursor(event.x(), event.y());
}
void NativeViewHost::Detach(bool destroyed) {
if (native_view_) {
if (!destroyed) {
Widget* widget = Widget::GetWidgetForNativeView(native_view_);
if (widget)
widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, NULL);
ClearFocus();
}
native_wrapper_->NativeViewDetaching(destroyed);
native_view_ = NULL;
}
}
void NativeViewHost::ClearFocus() {
FocusManager* focus_manager = GetFocusManager();
if (!focus_manager || !focus_manager->GetFocusedView())
return;
Widget::Widgets widgets;
Widget::GetAllChildWidgets(native_view(), &widgets);
for (Widget::Widgets::iterator i = widgets.begin(); i != widgets.end(); ++i) {
focus_manager->ViewRemoved((*i)->GetRootView());
if (!focus_manager->GetFocusedView())
return;
}
}
}