This source file includes following definitions.
- GetHierarchyRoot
- touch_dnd_enabled_
- OnGestureEvent
- native_view_accessibility_
- GetWidget
- GetWidget
- AddChildView
- AddChildViewAt
- ReorderChildView
- RemoveChildView
- RemoveAllChildViews
- Contains
- GetIndexOf
- SetBounds
- SetBoundsRect
- SetSize
- SetPosition
- SetX
- SetY
- GetContentsBounds
- GetLocalBounds
- GetLayerBoundsInPixel
- GetInsets
- GetVisibleBounds
- GetBoundsInScreen
- GetPreferredSize
- GetBaseline
- SizeToPreferredSize
- GetMinimumSize
- GetMaximumSize
- GetHeightForWidth
- SetVisible
- IsDrawn
- SetEnabled
- OnEnabledChanged
- GetTransform
- SetTransform
- SetPaintToLayer
- GetMirroredBounds
- GetMirroredPosition
- GetMirroredX
- GetMirroredXForRect
- GetMirroredXInView
- GetMirroredXWithWidthInView
- Layout
- InvalidateLayout
- GetLayoutManager
- SetLayoutManager
- GetClassName
- GetAncestorWithClassName
- GetAncestorWithClassName
- GetViewByID
- GetViewByID
- SetGroup
- GetGroup
- IsGroupFocusTraversable
- GetViewsInGroup
- GetSelectedViewForGroup
- ConvertPointToTarget
- ConvertRectToTarget
- ConvertPointToWidget
- ConvertPointFromWidget
- ConvertPointToScreen
- ConvertPointFromScreen
- ConvertRectToParent
- ConvertRectToWidget
- SchedulePaint
- SchedulePaintInRect
- Paint
- set_background
- SetBorder
- GetThemeProvider
- GetNativeTheme
- GetEventHandlerForPoint
- GetEventHandlerForRect
- GetTooltipHandlerForPoint
- GetCursor
- HitTestPoint
- HitTestRect
- IsMouseHovered
- OnMousePressed
- OnMouseDragged
- OnMouseReleased
- OnMouseCaptureLost
- OnMouseMoved
- OnMouseEntered
- OnMouseExited
- SetMouseHandler
- OnKeyPressed
- OnKeyReleased
- OnMouseWheel
- OnKeyEvent
- OnMouseEvent
- OnScrollEvent
- OnTouchEvent
- OnGestureEvent
- GetTextInputClient
- GetInputMethod
- GetInputMethod
- SetEventTargeter
- CanAcceptEvent
- GetParentTarget
- GetChildIterator
- GetEventTargeter
- AddAccelerator
- RemoveAccelerator
- ResetAccelerators
- AcceleratorPressed
- CanHandleAccelerators
- HasFocus
- GetNextFocusableView
- GetNextFocusableView
- GetPreviousFocusableView
- SetNextFocusableView
- SetFocusable
- IsFocusable
- IsAccessibilityFocusable
- SetAccessibilityFocusable
- GetFocusManager
- GetFocusManager
- RequestFocus
- SkipDefaultKeyEventProcessing
- GetFocusTraversable
- GetPaneFocusTraversable
- GetTooltipText
- GetTooltipTextOrigin
- ShowContextMenu
- ShouldShowContextMenuOnMousePress
- GetDropFormats
- AreDropTypesRequired
- CanDrop
- OnDragEntered
- OnDragUpdated
- OnDragExited
- OnPerformDrop
- OnDragDone
- ExceededDragThreshold
- GetNativeViewAccessible
- NotifyAccessibilityEvent
- ScrollRectToVisible
- GetPageScrollIncrement
- GetLineScrollIncrement
- OnBoundsChanged
- PreferredSizeChanged
- NeedsNotificationWhenVisibleBoundsChange
- OnVisibleBoundsChanged
- ViewHierarchyChanged
- VisibilityChanged
- NativeViewHierarchyChanged
- PaintChildren
- OnPaint
- OnPaintBackground
- OnPaintBorder
- SetFillsBoundsOpaquely
- CalculateOffsetToAncestorWithLayer
- UpdateParentLayer
- MoveLayerToParent
- UpdateLayerVisibility
- UpdateChildLayerVisibility
- UpdateChildLayerBounds
- OnPaintLayer
- OnDeviceScaleFactorChanged
- PrepareForLayerBoundsChange
- ReorderLayers
- ReorderChildLayers
- HasHitTestMask
- GetHitTestMask
- GetDragInfo
- OnFocus
- OnBlur
- Focus
- Blur
- TooltipTextChanged
- GetKeyboardContextMenuLocation
- GetDragOperations
- WriteDragData
- InDrag
- PrintViewGraph
- DoPrintViewGraph
- Reset
- PossibleDrag
- SchedulePaintBoundsChanged
- PaintCommon
- DoRemoveChildView
- PropagateRemoveNotifications
- PropagateAddNotifications
- PropagateNativeViewHierarchyChanged
- ViewHierarchyChangedImpl
- PropagateNativeThemeChanged
- PropagateVisibilityNotifications
- VisibilityChangedImpl
- BoundsChanged
- RegisterChildrenForVisibleBoundsNotification
- UnregisterChildrenForVisibleBoundsNotification
- RegisterForVisibleBoundsNotification
- UnregisterForVisibleBoundsNotification
- AddDescendantToNotify
- RemoveDescendantToNotify
- SetLayerBounds
- GetTransformRelativeTo
- ConvertPointForAncestor
- ConvertPointFromAncestor
- ConvertRectForAncestor
- ConvertRectFromAncestor
- CreateLayer
- UpdateParentLayers
- OrphanLayers
- ReparentLayer
- DestroyLayer
- ProcessMousePressed
- ProcessMouseDragged
- ProcessMouseReleased
- RegisterPendingAccelerators
- UnregisterAccelerators
- InitFocusSiblings
- PropagateThemeChanged
- PropagateLocaleChanged
- UpdateTooltip
- DoDrag
#define _USE_MATH_DEFINES
#include "ui/views/view.h"
#include <algorithm>
#include <cmath>
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/accessibility/ax_enums.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/ui_base_switches_util.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/events/event_target_iterator.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/interpolated_transform.h"
#include "ui/gfx/path.h"
#include "ui/gfx/point3_f.h"
#include "ui/gfx/point_conversions.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/transform.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/accessibility/native_view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/drag_controller.h"
#include "ui/views/focus/view_storage.h"
#include "ui/views/layout/layout_manager.h"
#include "ui/views/rect_based_targeting_utils.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/tooltip_manager.h"
#include "ui/views/widget/widget.h"
#if defined(OS_WIN)
#include "base/win/scoped_gdi_object.h"
#endif
namespace {
#if defined(OS_WIN)
const bool kContextMenuOnMousePress = false;
#else
const bool kContextMenuOnMousePress = true;
#endif
static const float kRectTargetOverlap = 0.6f;
const views::View* GetHierarchyRoot(const views::View* view) {
const views::View* root = view;
while (root && root->parent())
root = root->parent();
return root;
}
}
namespace views {
namespace internal {
class PostEventDispatchHandler : public ui::EventHandler {
public:
explicit PostEventDispatchHandler(View* owner)
: owner_(owner),
touch_dnd_enabled_(switches::IsTouchDragDropEnabled()) {
}
virtual ~PostEventDispatchHandler() {}
private:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
DCHECK_EQ(ui::EP_POSTTARGET, event->phase());
if (event->handled())
return;
if (touch_dnd_enabled_) {
if (event->type() == ui::ET_GESTURE_LONG_PRESS &&
(!owner_->drag_controller() ||
owner_->drag_controller()->CanStartDragForView(
owner_, event->location(), event->location()))) {
if (owner_->DoDrag(*event, event->location(),
ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH)) {
event->StopPropagation();
return;
}
}
}
if (owner_->context_menu_controller() &&
(event->type() == ui::ET_GESTURE_LONG_PRESS ||
event->type() == ui::ET_GESTURE_LONG_TAP ||
event->type() == ui::ET_GESTURE_TWO_FINGER_TAP)) {
gfx::Point location(event->location());
View::ConvertPointToScreen(owner_, &location);
owner_->ShowContextMenu(location, ui::MENU_SOURCE_TOUCH);
event->StopPropagation();
}
}
View* owner_;
bool touch_dnd_enabled_;
DISALLOW_COPY_AND_ASSIGN(PostEventDispatchHandler);
};
}
ViewsDelegate* ViewsDelegate::views_delegate = NULL;
const char View::kViewClassName[] = "View";
View::View()
: owned_by_client_(false),
id_(0),
group_(-1),
parent_(NULL),
visible_(true),
enabled_(true),
notify_enter_exit_on_child_(false),
registered_for_visible_bounds_notification_(false),
clip_insets_(0, 0, 0, 0),
needs_layout_(true),
flip_canvas_on_paint_for_rtl_ui_(false),
paint_to_layer_(false),
accelerator_focus_manager_(NULL),
registered_accelerator_count_(0),
next_focusable_view_(NULL),
previous_focusable_view_(NULL),
focusable_(false),
accessibility_focusable_(false),
context_menu_controller_(NULL),
drag_controller_(NULL),
post_dispatch_handler_(new internal::PostEventDispatchHandler(this)),
native_view_accessibility_(NULL) {
AddPostTargetHandler(post_dispatch_handler_.get());
}
View::~View() {
if (parent_)
parent_->RemoveChildView(this);
ViewStorage::GetInstance()->ViewRemoved(this);
for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i) {
(*i)->parent_ = NULL;
if (!(*i)->owned_by_client_)
delete *i;
}
if (native_view_accessibility_)
native_view_accessibility_->Destroy();
}
const Widget* View::GetWidget() const {
return parent_ ? parent_->GetWidget() : NULL;
}
Widget* View::GetWidget() {
return const_cast<Widget*>(const_cast<const View*>(this)->GetWidget());
}
void View::AddChildView(View* view) {
if (view->parent_ == this)
return;
AddChildViewAt(view, child_count());
}
void View::AddChildViewAt(View* view, int index) {
CHECK_NE(view, this) << "You cannot add a view as its own child";
DCHECK_GE(index, 0);
DCHECK_LE(index, child_count());
View* parent = view->parent_;
const ui::NativeTheme* old_theme = view->GetNativeTheme();
if (parent) {
if (parent == this) {
ReorderChildView(view, index);
return;
}
parent->DoRemoveChildView(view, true, true, false, this);
}
InitFocusSiblings(view, index);
view->parent_ = this;
children_.insert(children_.begin() + index, view);
ViewHierarchyChangedDetails details(true, this, view, parent);
for (View* v = this; v; v = v->parent_)
v->ViewHierarchyChangedImpl(false, details);
view->PropagateAddNotifications(details);
UpdateTooltip();
views::Widget* widget = GetWidget();
if (widget) {
RegisterChildrenForVisibleBoundsNotification(view);
const ui::NativeTheme* new_theme = widget->GetNativeTheme();
if (new_theme != old_theme)
PropagateNativeThemeChanged(new_theme);
if (view->visible())
view->SchedulePaint();
}
if (layout_manager_.get())
layout_manager_->ViewAdded(this, view);
ReorderLayers();
UpdateLayerVisibility();
}
void View::ReorderChildView(View* view, int index) {
DCHECK_EQ(view->parent_, this);
if (index < 0)
index = child_count() - 1;
else if (index >= child_count())
return;
if (children_[index] == view)
return;
const Views::iterator i(std::find(children_.begin(), children_.end(), view));
DCHECK(i != children_.end());
children_.erase(i);
View* next_focusable = view->next_focusable_view_;
View* prev_focusable = view->previous_focusable_view_;
if (prev_focusable)
prev_focusable->next_focusable_view_ = next_focusable;
if (next_focusable)
next_focusable->previous_focusable_view_ = prev_focusable;
InitFocusSiblings(view, index);
children_.insert(children_.begin() + index, view);
ReorderLayers();
}
void View::RemoveChildView(View* view) {
DoRemoveChildView(view, true, true, false, NULL);
}
void View::RemoveAllChildViews(bool delete_children) {
while (!children_.empty())
DoRemoveChildView(children_.front(), false, false, delete_children, NULL);
UpdateTooltip();
}
bool View::Contains(const View* view) const {
for (const View* v = view; v; v = v->parent_) {
if (v == this)
return true;
}
return false;
}
int View::GetIndexOf(const View* view) const {
Views::const_iterator i(std::find(children_.begin(), children_.end(), view));
return i != children_.end() ? static_cast<int>(i - children_.begin()) : -1;
}
void View::SetBounds(int x, int y, int width, int height) {
SetBoundsRect(gfx::Rect(x, y, std::max(0, width), std::max(0, height)));
}
void View::SetBoundsRect(const gfx::Rect& bounds) {
if (bounds == bounds_) {
if (needs_layout_) {
needs_layout_ = false;
Layout();
}
return;
}
if (visible_) {
SchedulePaintBoundsChanged(
bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
SCHEDULE_PAINT_SIZE_CHANGED);
}
gfx::Rect prev = bounds_;
bounds_ = bounds;
BoundsChanged(prev);
}
void View::SetSize(const gfx::Size& size) {
SetBounds(x(), y(), size.width(), size.height());
}
void View::SetPosition(const gfx::Point& position) {
SetBounds(position.x(), position.y(), width(), height());
}
void View::SetX(int x) {
SetBounds(x, y(), width(), height());
}
void View::SetY(int y) {
SetBounds(x(), y, width(), height());
}
gfx::Rect View::GetContentsBounds() const {
gfx::Rect contents_bounds(GetLocalBounds());
if (border_.get())
contents_bounds.Inset(border_->GetInsets());
return contents_bounds;
}
gfx::Rect View::GetLocalBounds() const {
return gfx::Rect(size());
}
gfx::Rect View::GetLayerBoundsInPixel() const {
return layer()->GetTargetBounds();
}
gfx::Insets View::GetInsets() const {
return border_.get() ? border_->GetInsets() : gfx::Insets();
}
gfx::Rect View::GetVisibleBounds() const {
if (!IsDrawn())
return gfx::Rect();
gfx::Rect vis_bounds(GetLocalBounds());
gfx::Rect ancestor_bounds;
const View* view = this;
gfx::Transform transform;
while (view != NULL && !vis_bounds.IsEmpty()) {
transform.ConcatTransform(view->GetTransform());
gfx::Transform translation;
translation.Translate(static_cast<float>(view->GetMirroredX()),
static_cast<float>(view->y()));
transform.ConcatTransform(translation);
vis_bounds = view->ConvertRectToParent(vis_bounds);
const View* ancestor = view->parent_;
if (ancestor != NULL) {
ancestor_bounds.SetRect(0, 0, ancestor->width(), ancestor->height());
vis_bounds.Intersect(ancestor_bounds);
} else if (!view->GetWidget()) {
return gfx::Rect();
}
view = ancestor;
}
if (vis_bounds.IsEmpty())
return vis_bounds;
gfx::RectF views_vis_bounds(vis_bounds);
transform.TransformRectReverse(&views_vis_bounds);
return gfx::ToEnclosingRect(views_vis_bounds);
}
gfx::Rect View::GetBoundsInScreen() const {
gfx::Point origin;
View::ConvertPointToScreen(this, &origin);
return gfx::Rect(origin, size());
}
gfx::Size View::GetPreferredSize() {
if (layout_manager_.get())
return layout_manager_->GetPreferredSize(this);
return gfx::Size();
}
int View::GetBaseline() const {
return -1;
}
void View::SizeToPreferredSize() {
gfx::Size prefsize = GetPreferredSize();
if ((prefsize.width() != width()) || (prefsize.height() != height()))
SetBounds(x(), y(), prefsize.width(), prefsize.height());
}
gfx::Size View::GetMinimumSize() {
return GetPreferredSize();
}
gfx::Size View::GetMaximumSize() {
return gfx::Size();
}
int View::GetHeightForWidth(int w) {
if (layout_manager_.get())
return layout_manager_->GetPreferredHeightForWidth(this, w);
return GetPreferredSize().height();
}
void View::SetVisible(bool visible) {
if (visible != visible_) {
if (visible_)
SchedulePaint();
visible_ = visible;
if (parent_)
parent_->ChildVisibilityChanged(this);
PropagateVisibilityNotifications(this, visible_);
UpdateLayerVisibility();
if (visible_)
SchedulePaint();
}
}
bool View::IsDrawn() const {
return visible_ && parent_ ? parent_->IsDrawn() : false;
}
void View::SetEnabled(bool enabled) {
if (enabled != enabled_) {
enabled_ = enabled;
OnEnabledChanged();
}
}
void View::OnEnabledChanged() {
SchedulePaint();
}
gfx::Transform View::GetTransform() const {
return layer() ? layer()->transform() : gfx::Transform();
}
void View::SetTransform(const gfx::Transform& transform) {
if (transform.IsIdentity()) {
if (layer()) {
layer()->SetTransform(transform);
if (!paint_to_layer_)
DestroyLayer();
} else {
}
} else {
if (!layer())
CreateLayer();
layer()->SetTransform(transform);
layer()->ScheduleDraw();
}
}
void View::SetPaintToLayer(bool paint_to_layer) {
paint_to_layer_ = paint_to_layer;
if (paint_to_layer_ && !layer()) {
CreateLayer();
} else if (!paint_to_layer_ && layer()) {
DestroyLayer();
}
}
gfx::Rect View::GetMirroredBounds() const {
gfx::Rect bounds(bounds_);
bounds.set_x(GetMirroredX());
return bounds;
}
gfx::Point View::GetMirroredPosition() const {
return gfx::Point(GetMirroredX(), y());
}
int View::GetMirroredX() const {
return parent_ ? parent_->GetMirroredXForRect(bounds_) : x();
}
int View::GetMirroredXForRect(const gfx::Rect& bounds) const {
return base::i18n::IsRTL() ?
(width() - bounds.x() - bounds.width()) : bounds.x();
}
int View::GetMirroredXInView(int x) const {
return base::i18n::IsRTL() ? width() - x : x;
}
int View::GetMirroredXWithWidthInView(int x, int w) const {
return base::i18n::IsRTL() ? width() - x - w : x;
}
void View::Layout() {
needs_layout_ = false;
if (layout_manager_.get())
layout_manager_->Layout(this);
for (int i = 0, count = child_count(); i < count; ++i) {
View* child = child_at(i);
if (child->needs_layout_ || !layout_manager_.get()) {
child->needs_layout_ = false;
child->Layout();
}
}
}
void View::InvalidateLayout() {
needs_layout_ = true;
if (parent_)
parent_->InvalidateLayout();
}
LayoutManager* View::GetLayoutManager() const {
return layout_manager_.get();
}
void View::SetLayoutManager(LayoutManager* layout_manager) {
if (layout_manager_.get())
layout_manager_->Uninstalled(this);
layout_manager_.reset(layout_manager);
if (layout_manager_.get())
layout_manager_->Installed(this);
}
const char* View::GetClassName() const {
return kViewClassName;
}
const View* View::GetAncestorWithClassName(const std::string& name) const {
for (const View* view = this; view; view = view->parent_) {
if (!strcmp(view->GetClassName(), name.c_str()))
return view;
}
return NULL;
}
View* View::GetAncestorWithClassName(const std::string& name) {
return const_cast<View*>(const_cast<const View*>(this)->
GetAncestorWithClassName(name));
}
const View* View::GetViewByID(int id) const {
if (id == id_)
return const_cast<View*>(this);
for (int i = 0, count = child_count(); i < count; ++i) {
const View* view = child_at(i)->GetViewByID(id);
if (view)
return view;
}
return NULL;
}
View* View::GetViewByID(int id) {
return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id));
}
void View::SetGroup(int gid) {
DCHECK(group_ == -1 || group_ == gid);
group_ = gid;
}
int View::GetGroup() const {
return group_;
}
bool View::IsGroupFocusTraversable() const {
return true;
}
void View::GetViewsInGroup(int group, Views* views) {
if (group_ == group)
views->push_back(this);
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->GetViewsInGroup(group, views);
}
View* View::GetSelectedViewForGroup(int group) {
Views views;
GetWidget()->GetRootView()->GetViewsInGroup(group, &views);
return views.empty() ? NULL : views[0];
}
void View::ConvertPointToTarget(const View* source,
const View* target,
gfx::Point* point) {
DCHECK(source);
DCHECK(target);
if (source == target)
return;
const View* root = GetHierarchyRoot(target);
CHECK_EQ(GetHierarchyRoot(source), root);
if (source != root)
source->ConvertPointForAncestor(root, point);
if (target != root)
target->ConvertPointFromAncestor(root, point);
}
void View::ConvertRectToTarget(const View* source,
const View* target,
gfx::RectF* rect) {
DCHECK(source);
DCHECK(target);
if (source == target)
return;
const View* root = GetHierarchyRoot(target);
CHECK_EQ(GetHierarchyRoot(source), root);
if (source != root)
source->ConvertRectForAncestor(root, rect);
if (target != root)
target->ConvertRectFromAncestor(root, rect);
}
void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
src->ConvertPointForAncestor(NULL, p);
}
void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) {
DCHECK(dest);
DCHECK(p);
dest->ConvertPointFromAncestor(NULL, p);
}
void View::ConvertPointToScreen(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
const Widget* widget = src->GetWidget();
if (widget) {
ConvertPointToWidget(src, p);
*p += widget->GetClientAreaBoundsInScreen().OffsetFromOrigin();
}
}
void View::ConvertPointFromScreen(const View* dst, gfx::Point* p) {
DCHECK(dst);
DCHECK(p);
const views::Widget* widget = dst->GetWidget();
if (!widget)
return;
*p -= widget->GetClientAreaBoundsInScreen().OffsetFromOrigin();
views::View::ConvertPointFromWidget(dst, p);
}
gfx::Rect View::ConvertRectToParent(const gfx::Rect& rect) const {
gfx::RectF x_rect = rect;
GetTransform().TransformRect(&x_rect);
x_rect.Offset(GetMirroredPosition().OffsetFromOrigin());
return gfx::ToEnclosingRect(x_rect);
}
gfx::Rect View::ConvertRectToWidget(const gfx::Rect& rect) const {
gfx::Rect x_rect = rect;
for (const View* v = this; v; v = v->parent_)
x_rect = v->ConvertRectToParent(x_rect);
return x_rect;
}
void View::SchedulePaint() {
SchedulePaintInRect(GetLocalBounds());
}
void View::SchedulePaintInRect(const gfx::Rect& rect) {
if (!visible_)
return;
if (layer()) {
layer()->SchedulePaint(rect);
} else if (parent_) {
parent_->SchedulePaintInRect(ConvertRectToParent(rect));
}
}
void View::Paint(gfx::Canvas* canvas) {
TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
gfx::ScopedCanvas scoped_canvas(canvas);
gfx::Rect clip_rect = bounds();
clip_rect.Inset(clip_insets_);
if (parent_)
clip_rect.set_x(parent_->GetMirroredXForRect(clip_rect));
canvas->ClipRect(clip_rect);
if (canvas->IsClipEmpty())
return;
canvas->Translate(GetMirroredPosition().OffsetFromOrigin());
canvas->Transform(GetTransform());
PaintCommon(canvas);
}
void View::set_background(Background* b) {
background_.reset(b);
}
void View::SetBorder(scoped_ptr<Border> b) { border_ = b.Pass(); }
ui::ThemeProvider* View::GetThemeProvider() const {
const Widget* widget = GetWidget();
return widget ? widget->GetThemeProvider() : NULL;
}
const ui::NativeTheme* View::GetNativeTheme() const {
const Widget* widget = GetWidget();
return widget ? widget->GetNativeTheme() : ui::NativeTheme::instance();
}
View* View::GetEventHandlerForPoint(const gfx::Point& point) {
return GetEventHandlerForRect(gfx::Rect(point, gfx::Size(1, 1)));
}
View* View::GetEventHandlerForRect(const gfx::Rect& rect) {
View* rect_view = NULL;
int rect_view_distance = INT_MAX;
View* point_view = NULL;
for (int i = child_count() - 1; i >= 0; --i) {
View* child = child_at(i);
if (!child->visible())
continue;
gfx::RectF rect_in_child_coords_f(rect);
ConvertRectToTarget(this, child, &rect_in_child_coords_f);
gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect(
rect_in_child_coords_f);
if (!child->HitTestRect(rect_in_child_coords))
continue;
View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
if (views::UsePointBasedTargeting(rect))
return cur_view;
gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds());
ConvertRectToTarget(cur_view, this, &cur_view_bounds_f);
gfx::Rect cur_view_bounds = gfx::ToEnclosingRect(
cur_view_bounds_f);
if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) {
gfx::Point touch_center(rect.CenterPoint());
int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center,
cur_view_bounds);
if (!rect_view || cur_dist < rect_view_distance) {
rect_view = cur_view;
rect_view_distance = cur_dist;
}
} else if (!rect_view && !point_view) {
gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint());
if (child->HitTestPoint(point_in_child_coords))
point_view = child->GetEventHandlerForPoint(point_in_child_coords);
}
}
if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view))
return this;
gfx::Rect local_bounds(GetLocalBounds());
if (views::PercentCoveredBy(local_bounds, rect) >= kRectTargetOverlap) {
gfx::Point touch_center(rect.CenterPoint());
int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center,
local_bounds);
if (!rect_view || cur_dist < rect_view_distance)
rect_view = this;
}
return rect_view ? rect_view : point_view;
}
View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
if (!HitTestPoint(point))
return NULL;
for (int i = child_count() - 1; i >= 0; --i) {
View* child = child_at(i);
if (!child->visible())
continue;
gfx::Point point_in_child_coords(point);
ConvertPointToTarget(this, child, &point_in_child_coords);
View* handler = child->GetTooltipHandlerForPoint(point_in_child_coords);
if (handler)
return handler;
}
return this;
}
gfx::NativeCursor View::GetCursor(const ui::MouseEvent& event) {
#if defined(OS_WIN)
static ui::Cursor arrow;
if (!arrow.platform())
arrow.SetPlatformCursor(LoadCursor(NULL, IDC_ARROW));
return arrow;
#else
return gfx::kNullCursor;
#endif
}
bool View::HitTestPoint(const gfx::Point& point) const {
return HitTestRect(gfx::Rect(point, gfx::Size(1, 1)));
}
bool View::HitTestRect(const gfx::Rect& rect) const {
if (GetLocalBounds().Intersects(rect)) {
if (HasHitTestMask()) {
gfx::Path mask;
HitTestSource source = HIT_TEST_SOURCE_MOUSE;
if (!views::UsePointBasedTargeting(rect))
source = HIT_TEST_SOURCE_TOUCH;
GetHitTestMask(source, &mask);
SkRegion clip_region;
clip_region.setRect(0, 0, width(), height());
SkRegion mask_region;
return mask_region.setPath(mask, clip_region) &&
mask_region.intersects(RectToSkIRect(rect));
}
return true;
}
return false;
}
bool View::IsMouseHovered() {
if (!GetWidget())
return false;
if (!GetWidget()->IsMouseEventsEnabled())
return false;
gfx::Point cursor_pos(gfx::Screen::GetScreenFor(
GetWidget()->GetNativeView())->GetCursorScreenPoint());
ConvertPointFromScreen(this, &cursor_pos);
return HitTestPoint(cursor_pos);
}
bool View::OnMousePressed(const ui::MouseEvent& event) {
return false;
}
bool View::OnMouseDragged(const ui::MouseEvent& event) {
return false;
}
void View::OnMouseReleased(const ui::MouseEvent& event) {
}
void View::OnMouseCaptureLost() {
}
void View::OnMouseMoved(const ui::MouseEvent& event) {
}
void View::OnMouseEntered(const ui::MouseEvent& event) {
}
void View::OnMouseExited(const ui::MouseEvent& event) {
}
void View::SetMouseHandler(View* new_mouse_handler) {
if (parent_)
parent_->SetMouseHandler(new_mouse_handler);
}
bool View::OnKeyPressed(const ui::KeyEvent& event) {
return false;
}
bool View::OnKeyReleased(const ui::KeyEvent& event) {
return false;
}
bool View::OnMouseWheel(const ui::MouseWheelEvent& event) {
return false;
}
void View::OnKeyEvent(ui::KeyEvent* event) {
bool consumed = (event->type() == ui::ET_KEY_PRESSED) ? OnKeyPressed(*event) :
OnKeyReleased(*event);
if (consumed)
event->StopPropagation();
}
void View::OnMouseEvent(ui::MouseEvent* event) {
switch (event->type()) {
case ui::ET_MOUSE_PRESSED:
if (ProcessMousePressed(*event))
event->SetHandled();
return;
case ui::ET_MOUSE_MOVED:
if ((event->flags() & (ui::EF_LEFT_MOUSE_BUTTON |
ui::EF_RIGHT_MOUSE_BUTTON |
ui::EF_MIDDLE_MOUSE_BUTTON)) == 0) {
OnMouseMoved(*event);
return;
}
case ui::ET_MOUSE_DRAGGED:
if (ProcessMouseDragged(*event))
event->SetHandled();
return;
case ui::ET_MOUSE_RELEASED:
ProcessMouseReleased(*event);
return;
case ui::ET_MOUSEWHEEL:
if (OnMouseWheel(*static_cast<ui::MouseWheelEvent*>(event)))
event->SetHandled();
break;
case ui::ET_MOUSE_ENTERED:
OnMouseEntered(*event);
break;
case ui::ET_MOUSE_EXITED:
OnMouseExited(*event);
break;
default:
return;
}
}
void View::OnScrollEvent(ui::ScrollEvent* event) {
}
void View::OnTouchEvent(ui::TouchEvent* event) {
NOTREACHED() << "Views should not receive touch events.";
}
void View::OnGestureEvent(ui::GestureEvent* event) {
}
ui::TextInputClient* View::GetTextInputClient() {
return NULL;
}
InputMethod* View::GetInputMethod() {
Widget* widget = GetWidget();
return widget ? widget->GetInputMethod() : NULL;
}
const InputMethod* View::GetInputMethod() const {
const Widget* widget = GetWidget();
return widget ? widget->GetInputMethod() : NULL;
}
scoped_ptr<ui::EventTargeter>
View::SetEventTargeter(scoped_ptr<ui::EventTargeter> targeter) {
scoped_ptr<ui::EventTargeter> old_targeter = targeter_.Pass();
targeter_ = targeter.Pass();
return old_targeter.Pass();
}
bool View::CanAcceptEvent(const ui::Event& event) {
return IsDrawn();
}
ui::EventTarget* View::GetParentTarget() {
return parent_;
}
scoped_ptr<ui::EventTargetIterator> View::GetChildIterator() const {
return scoped_ptr<ui::EventTargetIterator>(
new ui::EventTargetIteratorImpl<View>(children_));
}
ui::EventTargeter* View::GetEventTargeter() {
return targeter_.get();
}
void View::AddAccelerator(const ui::Accelerator& accelerator) {
if (!accelerators_.get())
accelerators_.reset(new std::vector<ui::Accelerator>());
if (std::find(accelerators_->begin(), accelerators_->end(), accelerator) ==
accelerators_->end()) {
accelerators_->push_back(accelerator);
}
RegisterPendingAccelerators();
}
void View::RemoveAccelerator(const ui::Accelerator& accelerator) {
if (!accelerators_.get()) {
NOTREACHED() << "Removing non-existing accelerator";
return;
}
std::vector<ui::Accelerator>::iterator i(
std::find(accelerators_->begin(), accelerators_->end(), accelerator));
if (i == accelerators_->end()) {
NOTREACHED() << "Removing non-existing accelerator";
return;
}
size_t index = i - accelerators_->begin();
accelerators_->erase(i);
if (index >= registered_accelerator_count_) {
return;
}
--registered_accelerator_count_;
if (GetWidget() && accelerator_focus_manager_)
accelerator_focus_manager_->UnregisterAccelerator(accelerator, this);
}
void View::ResetAccelerators() {
if (accelerators_.get())
UnregisterAccelerators(false);
}
bool View::AcceleratorPressed(const ui::Accelerator& accelerator) {
return false;
}
bool View::CanHandleAccelerators() const {
return enabled() && IsDrawn() && GetWidget() && GetWidget()->IsVisible();
}
bool View::HasFocus() const {
const FocusManager* focus_manager = GetFocusManager();
return focus_manager && (focus_manager->GetFocusedView() == this);
}
View* View::GetNextFocusableView() {
return next_focusable_view_;
}
const View* View::GetNextFocusableView() const {
return next_focusable_view_;
}
View* View::GetPreviousFocusableView() {
return previous_focusable_view_;
}
void View::SetNextFocusableView(View* view) {
if (view)
view->previous_focusable_view_ = this;
next_focusable_view_ = view;
}
void View::SetFocusable(bool focusable) {
if (focusable_ == focusable)
return;
focusable_ = focusable;
}
bool View::IsFocusable() const {
return focusable_ && enabled_ && IsDrawn();
}
bool View::IsAccessibilityFocusable() const {
return (focusable_ || accessibility_focusable_) && enabled_ && IsDrawn();
}
void View::SetAccessibilityFocusable(bool accessibility_focusable) {
if (accessibility_focusable_ == accessibility_focusable)
return;
accessibility_focusable_ = accessibility_focusable;
}
FocusManager* View::GetFocusManager() {
Widget* widget = GetWidget();
return widget ? widget->GetFocusManager() : NULL;
}
const FocusManager* View::GetFocusManager() const {
const Widget* widget = GetWidget();
return widget ? widget->GetFocusManager() : NULL;
}
void View::RequestFocus() {
FocusManager* focus_manager = GetFocusManager();
if (focus_manager && IsFocusable())
focus_manager->SetFocusedView(this);
}
bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
return false;
}
FocusTraversable* View::GetFocusTraversable() {
return NULL;
}
FocusTraversable* View::GetPaneFocusTraversable() {
return NULL;
}
bool View::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const {
return false;
}
bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) const {
return false;
}
void View::ShowContextMenu(const gfx::Point& p,
ui::MenuSourceType source_type) {
if (!context_menu_controller_)
return;
context_menu_controller_->ShowContextMenuForView(this, p, source_type);
}
bool View::ShouldShowContextMenuOnMousePress() {
return kContextMenuOnMousePress;
}
bool View::GetDropFormats(
int* formats,
std::set<OSExchangeData::CustomFormat>* custom_formats) {
return false;
}
bool View::AreDropTypesRequired() {
return false;
}
bool View::CanDrop(const OSExchangeData& data) {
return false;
}
void View::OnDragEntered(const ui::DropTargetEvent& event) {
}
int View::OnDragUpdated(const ui::DropTargetEvent& event) {
return ui::DragDropTypes::DRAG_NONE;
}
void View::OnDragExited() {
}
int View::OnPerformDrop(const ui::DropTargetEvent& event) {
return ui::DragDropTypes::DRAG_NONE;
}
void View::OnDragDone() {
}
bool View::ExceededDragThreshold(const gfx::Vector2d& delta) {
return (abs(delta.x()) > GetHorizontalDragThreshold() ||
abs(delta.y()) > GetVerticalDragThreshold());
}
gfx::NativeViewAccessible View::GetNativeViewAccessible() {
if (!native_view_accessibility_)
native_view_accessibility_ = NativeViewAccessibility::Create(this);
if (native_view_accessibility_)
return native_view_accessibility_->GetNativeObject();
return NULL;
}
void View::NotifyAccessibilityEvent(
ui::AXEvent event_type,
bool send_native_event) {
if (ViewsDelegate::views_delegate)
ViewsDelegate::views_delegate->NotifyAccessibilityEvent(this, event_type);
if (send_native_event && GetWidget()) {
if (!native_view_accessibility_)
native_view_accessibility_ = NativeViewAccessibility::Create(this);
if (native_view_accessibility_)
native_view_accessibility_->NotifyAccessibilityEvent(event_type);
}
}
void View::ScrollRectToVisible(const gfx::Rect& rect) {
if (parent_) {
gfx::Rect scroll_rect(rect);
scroll_rect.Offset(GetMirroredX(), y());
parent_->ScrollRectToVisible(scroll_rect);
}
}
int View::GetPageScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
}
int View::GetLineScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
}
void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
}
void View::PreferredSizeChanged() {
InvalidateLayout();
if (parent_)
parent_->ChildPreferredSizeChanged(this);
}
bool View::NeedsNotificationWhenVisibleBoundsChange() const {
return false;
}
void View::OnVisibleBoundsChanged() {
}
void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails& details) {
}
void View::VisibilityChanged(View* starting_from, bool is_visible) {
}
void View::NativeViewHierarchyChanged() {
FocusManager* focus_manager = GetFocusManager();
if (accelerator_focus_manager_ != focus_manager) {
UnregisterAccelerators(true);
if (focus_manager)
RegisterPendingAccelerators();
}
}
void View::PaintChildren(gfx::Canvas* canvas) {
TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
for (int i = 0, count = child_count(); i < count; ++i)
if (!child_at(i)->layer())
child_at(i)->Paint(canvas);
}
void View::OnPaint(gfx::Canvas* canvas) {
TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
OnPaintBackground(canvas);
OnPaintBorder(canvas);
}
void View::OnPaintBackground(gfx::Canvas* canvas) {
if (background_.get()) {
TRACE_EVENT2("views", "View::OnPaintBackground",
"width", canvas->sk_canvas()->getDevice()->width(),
"height", canvas->sk_canvas()->getDevice()->height());
background_->Paint(canvas, this);
}
}
void View::OnPaintBorder(gfx::Canvas* canvas) {
if (border_.get()) {
TRACE_EVENT2("views", "View::OnPaintBorder",
"width", canvas->sk_canvas()->getDevice()->width(),
"height", canvas->sk_canvas()->getDevice()->height());
border_->Paint(*this, canvas);
}
}
void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) {
if (layer())
layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely);
}
gfx::Vector2d View::CalculateOffsetToAncestorWithLayer(
ui::Layer** layer_parent) {
if (layer()) {
if (layer_parent)
*layer_parent = layer();
return gfx::Vector2d();
}
if (!parent_)
return gfx::Vector2d();
return gfx::Vector2d(GetMirroredX(), y()) +
parent_->CalculateOffsetToAncestorWithLayer(layer_parent);
}
void View::UpdateParentLayer() {
if (!layer())
return;
ui::Layer* parent_layer = NULL;
gfx::Vector2d offset(GetMirroredX(), y());
if (parent_)
offset += parent_->CalculateOffsetToAncestorWithLayer(&parent_layer);
ReparentLayer(offset, parent_layer);
}
void View::MoveLayerToParent(ui::Layer* parent_layer,
const gfx::Point& point) {
gfx::Point local_point(point);
if (parent_layer != layer())
local_point.Offset(GetMirroredX(), y());
if (layer() && parent_layer != layer()) {
parent_layer->Add(layer());
SetLayerBounds(gfx::Rect(local_point.x(), local_point.y(),
width(), height()));
} else {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->MoveLayerToParent(parent_layer, local_point);
}
}
void View::UpdateLayerVisibility() {
bool visible = visible_;
for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_)
visible = v->visible();
UpdateChildLayerVisibility(visible);
}
void View::UpdateChildLayerVisibility(bool ancestor_visible) {
if (layer()) {
layer()->SetVisible(ancestor_visible && visible_);
} else {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->UpdateChildLayerVisibility(ancestor_visible && visible_);
}
}
void View::UpdateChildLayerBounds(const gfx::Vector2d& offset) {
if (layer()) {
SetLayerBounds(GetLocalBounds() + offset);
} else {
for (int i = 0, count = child_count(); i < count; ++i) {
View* child = child_at(i);
child->UpdateChildLayerBounds(
offset + gfx::Vector2d(child->GetMirroredX(), child->y()));
}
}
}
void View::OnPaintLayer(gfx::Canvas* canvas) {
if (!layer() || !layer()->fills_bounds_opaquely())
canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
PaintCommon(canvas);
}
void View::OnDeviceScaleFactorChanged(float device_scale_factor) {
}
base::Closure View::PrepareForLayerBoundsChange() {
return base::Closure();
}
void View::ReorderLayers() {
View* v = this;
while (v && !v->layer())
v = v->parent();
Widget* widget = GetWidget();
if (!v) {
if (widget) {
ui::Layer* layer = widget->GetLayer();
if (layer)
widget->GetRootView()->ReorderChildLayers(layer);
}
} else {
v->ReorderChildLayers(v->layer());
}
if (widget) {
widget->ReorderNativeViews();
}
}
void View::ReorderChildLayers(ui::Layer* parent_layer) {
if (layer() && layer() != parent_layer) {
DCHECK_EQ(parent_layer, layer()->parent());
parent_layer->StackAtBottom(layer());
} else {
for (Views::reverse_iterator it(children_.rbegin());
it != children_.rend(); ++it) {
(*it)->ReorderChildLayers(parent_layer);
}
}
}
bool View::HasHitTestMask() const {
return false;
}
void View::GetHitTestMask(HitTestSource source, gfx::Path* mask) const {
DCHECK(mask);
}
View::DragInfo* View::GetDragInfo() {
return parent_ ? parent_->GetDragInfo() : NULL;
}
void View::OnFocus() {
FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
focus_manager->ClearNativeFocus();
NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
}
void View::OnBlur() {
}
void View::Focus() {
OnFocus();
}
void View::Blur() {
OnBlur();
}
void View::TooltipTextChanged() {
Widget* widget = GetWidget();
if (widget && widget->GetTooltipManager())
widget->GetTooltipManager()->TooltipTextChanged(this);
}
gfx::Point View::GetKeyboardContextMenuLocation() {
gfx::Rect vis_bounds = GetVisibleBounds();
gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
vis_bounds.y() + vis_bounds.height() / 2);
ConvertPointToScreen(this, &screen_point);
return screen_point;
}
int View::GetDragOperations(const gfx::Point& press_pt) {
return drag_controller_ ?
drag_controller_->GetDragOperationsForView(this, press_pt) :
ui::DragDropTypes::DRAG_NONE;
}
void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) {
DCHECK(drag_controller_);
drag_controller_->WriteDragDataForView(this, press_pt, data);
}
bool View::InDrag() {
Widget* widget = GetWidget();
return widget ? widget->dragged_view() == this : false;
}
#if !defined(NDEBUG)
std::string View::PrintViewGraph(bool first) {
return DoPrintViewGraph(first, this);
}
std::string View::DoPrintViewGraph(bool first, View* view_with_children) {
const size_t kMaxPointerStringLength = 19;
std::string result;
if (first)
result.append("digraph {\n");
char p[kMaxPointerStringLength];
const std::string class_name(GetClassName());
size_t base_name_index = class_name.find_last_of('/');
if (base_name_index == std::string::npos)
base_name_index = 0;
else
base_name_index++;
char bounds_buffer[512];
base::snprintf(p, arraysize(bounds_buffer), "%p", view_with_children);
result.append(" N");
result.append(p + 2);
result.append(" [label=\"");
result.append(class_name.substr(base_name_index).c_str());
base::snprintf(bounds_buffer,
arraysize(bounds_buffer),
"\\n bounds: (%d, %d), (%dx%d)",
bounds().x(),
bounds().y(),
bounds().width(),
bounds().height());
result.append(bounds_buffer);
gfx::DecomposedTransform decomp;
if (!GetTransform().IsIdentity() &&
gfx::DecomposeTransform(&decomp, GetTransform())) {
base::snprintf(bounds_buffer,
arraysize(bounds_buffer),
"\\n translation: (%f, %f)",
decomp.translate[0],
decomp.translate[1]);
result.append(bounds_buffer);
base::snprintf(bounds_buffer,
arraysize(bounds_buffer),
"\\n rotation: %3.2f",
std::acos(decomp.quaternion[3]) * 360.0 / M_PI);
result.append(bounds_buffer);
base::snprintf(bounds_buffer,
arraysize(bounds_buffer),
"\\n scale: (%2.4f, %2.4f)",
decomp.scale[0],
decomp.scale[1]);
result.append(bounds_buffer);
}
result.append("\"");
if (!parent_)
result.append(", shape=box");
if (layer()) {
if (layer()->texture())
result.append(", color=green");
else
result.append(", color=red");
if (layer()->fills_bounds_opaquely())
result.append(", style=filled");
}
result.append("]\n");
if (parent_) {
char pp[kMaxPointerStringLength];
base::snprintf(pp, kMaxPointerStringLength, "%p", parent_);
result.append(" N");
result.append(pp + 2);
result.append(" -> N");
result.append(p + 2);
result.append("\n");
}
for (int i = 0, count = view_with_children->child_count(); i < count; ++i)
result.append(view_with_children->child_at(i)->PrintViewGraph(false));
if (first)
result.append("}\n");
return result;
}
#endif
void View::DragInfo::Reset() {
possible_drag = false;
start_pt = gfx::Point();
}
void View::DragInfo::PossibleDrag(const gfx::Point& p) {
possible_drag = true;
start_pt = p;
}
void View::SchedulePaintBoundsChanged(SchedulePaintType type) {
if (!layer() || type == SCHEDULE_PAINT_SIZE_CHANGED) {
SchedulePaint();
} else if (parent_ && type == SCHEDULE_PAINT_SIZE_SAME) {
layer()->ScheduleDraw();
}
}
void View::PaintCommon(gfx::Canvas* canvas) {
if (!visible_)
return;
{
gfx::ScopedCanvas scoped(canvas);
if (FlipCanvasOnPaintForRTLUI()) {
canvas->Translate(gfx::Vector2d(width(), 0));
canvas->Scale(-1, 1);
}
OnPaint(canvas);
}
PaintChildren(canvas);
}
void View::DoRemoveChildView(View* view,
bool update_focus_cycle,
bool update_tool_tip,
bool delete_removed_view,
View* new_parent) {
DCHECK(view);
const Views::iterator i(std::find(children_.begin(), children_.end(), view));
scoped_ptr<View> view_to_be_deleted;
if (i != children_.end()) {
if (update_focus_cycle) {
View* next_focusable = view->next_focusable_view_;
View* prev_focusable = view->previous_focusable_view_;
if (prev_focusable)
prev_focusable->next_focusable_view_ = next_focusable;
if (next_focusable)
next_focusable->previous_focusable_view_ = prev_focusable;
}
if (GetWidget()) {
UnregisterChildrenForVisibleBoundsNotification(view);
if (view->visible())
view->SchedulePaint();
}
view->PropagateRemoveNotifications(this, new_parent);
view->parent_ = NULL;
view->UpdateLayerVisibility();
if (delete_removed_view && !view->owned_by_client_)
view_to_be_deleted.reset(view);
children_.erase(i);
}
if (update_tool_tip)
UpdateTooltip();
if (layout_manager_.get())
layout_manager_->ViewRemoved(this, view);
}
void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->PropagateRemoveNotifications(old_parent, new_parent);
ViewHierarchyChangedDetails details(false, old_parent, this, new_parent);
for (View* v = this; v; v = v->parent_)
v->ViewHierarchyChangedImpl(true, details);
}
void View::PropagateAddNotifications(
const ViewHierarchyChangedDetails& details) {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->PropagateAddNotifications(details);
ViewHierarchyChangedImpl(true, details);
}
void View::PropagateNativeViewHierarchyChanged() {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->PropagateNativeViewHierarchyChanged();
NativeViewHierarchyChanged();
}
void View::ViewHierarchyChangedImpl(
bool register_accelerators,
const ViewHierarchyChangedDetails& details) {
if (register_accelerators) {
if (details.is_add) {
if (GetFocusManager())
RegisterPendingAccelerators();
} else {
if (details.child == this)
UnregisterAccelerators(true);
}
}
if (details.is_add && layer() && !layer()->parent()) {
UpdateParentLayer();
Widget* widget = GetWidget();
if (widget)
widget->UpdateRootLayers();
} else if (!details.is_add && details.child == this) {
OrphanLayers();
Widget* widget = GetWidget();
if (widget)
widget->UpdateRootLayers();
}
ViewHierarchyChanged(details);
details.parent->needs_layout_ = true;
}
void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->PropagateNativeThemeChanged(theme);
OnNativeThemeChanged(theme);
}
void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->PropagateVisibilityNotifications(start, is_visible);
VisibilityChangedImpl(start, is_visible);
}
void View::VisibilityChangedImpl(View* starting_from, bool is_visible) {
VisibilityChanged(starting_from, is_visible);
}
void View::BoundsChanged(const gfx::Rect& previous_bounds) {
if (visible_) {
SchedulePaintBoundsChanged(
bounds_.size() == previous_bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
SCHEDULE_PAINT_SIZE_CHANGED);
}
if (layer()) {
if (parent_) {
SetLayerBounds(GetLocalBounds() +
gfx::Vector2d(GetMirroredX(), y()) +
parent_->CalculateOffsetToAncestorWithLayer(NULL));
} else {
SetLayerBounds(bounds_);
}
} else {
UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL));
}
OnBoundsChanged(previous_bounds);
if (previous_bounds.size() != size()) {
needs_layout_ = false;
Layout();
}
if (NeedsNotificationWhenVisibleBoundsChange())
OnVisibleBoundsChanged();
if (descendants_to_notify_.get()) {
for (Views::iterator i(descendants_to_notify_->begin());
i != descendants_to_notify_->end(); ++i) {
(*i)->OnVisibleBoundsChanged();
}
}
}
void View::RegisterChildrenForVisibleBoundsNotification(View* view) {
if (view->NeedsNotificationWhenVisibleBoundsChange())
view->RegisterForVisibleBoundsNotification();
for (int i = 0; i < view->child_count(); ++i)
RegisterChildrenForVisibleBoundsNotification(view->child_at(i));
}
void View::UnregisterChildrenForVisibleBoundsNotification(View* view) {
if (view->NeedsNotificationWhenVisibleBoundsChange())
view->UnregisterForVisibleBoundsNotification();
for (int i = 0; i < view->child_count(); ++i)
UnregisterChildrenForVisibleBoundsNotification(view->child_at(i));
}
void View::RegisterForVisibleBoundsNotification() {
if (registered_for_visible_bounds_notification_)
return;
registered_for_visible_bounds_notification_ = true;
for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
ancestor->AddDescendantToNotify(this);
}
void View::UnregisterForVisibleBoundsNotification() {
if (!registered_for_visible_bounds_notification_)
return;
registered_for_visible_bounds_notification_ = false;
for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
ancestor->RemoveDescendantToNotify(this);
}
void View::AddDescendantToNotify(View* view) {
DCHECK(view);
if (!descendants_to_notify_.get())
descendants_to_notify_.reset(new Views);
descendants_to_notify_->push_back(view);
}
void View::RemoveDescendantToNotify(View* view) {
DCHECK(view && descendants_to_notify_.get());
Views::iterator i(std::find(
descendants_to_notify_->begin(), descendants_to_notify_->end(), view));
DCHECK(i != descendants_to_notify_->end());
descendants_to_notify_->erase(i);
if (descendants_to_notify_->empty())
descendants_to_notify_.reset();
}
void View::SetLayerBounds(const gfx::Rect& bounds) {
layer()->SetBounds(bounds);
}
bool View::GetTransformRelativeTo(const View* ancestor,
gfx::Transform* transform) const {
const View* p = this;
while (p && p != ancestor) {
transform->ConcatTransform(p->GetTransform());
gfx::Transform translation;
translation.Translate(static_cast<float>(p->GetMirroredX()),
static_cast<float>(p->y()));
transform->ConcatTransform(translation);
p = p->parent_;
}
return p == ancestor;
}
bool View::ConvertPointForAncestor(const View* ancestor,
gfx::Point* point) const {
gfx::Transform trans;
bool result = GetTransformRelativeTo(ancestor, &trans);
gfx::Point3F p(*point);
trans.TransformPoint(&p);
*point = gfx::ToFlooredPoint(p.AsPointF());
return result;
}
bool View::ConvertPointFromAncestor(const View* ancestor,
gfx::Point* point) const {
gfx::Transform trans;
bool result = GetTransformRelativeTo(ancestor, &trans);
gfx::Point3F p(*point);
trans.TransformPointReverse(&p);
*point = gfx::ToFlooredPoint(p.AsPointF());
return result;
}
bool View::ConvertRectForAncestor(const View* ancestor,
gfx::RectF* rect) const {
gfx::Transform trans;
bool result = GetTransformRelativeTo(ancestor, &trans);
trans.TransformRect(rect);
return result;
}
bool View::ConvertRectFromAncestor(const View* ancestor,
gfx::RectF* rect) const {
gfx::Transform trans;
bool result = GetTransformRelativeTo(ancestor, &trans);
trans.TransformRectReverse(rect);
return result;
}
void View::CreateLayer() {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->UpdateChildLayerVisibility(true);
SetLayer(new ui::Layer());
layer()->set_delegate(this);
#if !defined(NDEBUG)
layer()->set_name(GetClassName());
#endif
UpdateParentLayers();
UpdateLayerVisibility();
if (parent())
parent()->ReorderLayers();
Widget* widget = GetWidget();
if (widget)
widget->UpdateRootLayers();
}
void View::UpdateParentLayers() {
if (layer() && !layer()->parent()) {
UpdateParentLayer();
} else {
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->UpdateParentLayers();
}
}
void View::OrphanLayers() {
if (layer()) {
if (layer()->parent())
layer()->parent()->Remove(layer());
return;
}
for (int i = 0, count = child_count(); i < count; ++i)
child_at(i)->OrphanLayers();
}
void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) {
layer()->SetBounds(GetLocalBounds() + offset);
DCHECK_NE(layer(), parent_layer);
if (parent_layer)
parent_layer->Add(layer());
layer()->SchedulePaint(GetLocalBounds());
MoveLayerToParent(layer(), gfx::Point());
}
void View::DestroyLayer() {
ui::Layer* new_parent = layer()->parent();
std::vector<ui::Layer*> children = layer()->children();
for (size_t i = 0; i < children.size(); ++i) {
layer()->Remove(children[i]);
if (new_parent)
new_parent->Add(children[i]);
}
LayerOwner::DestroyLayer();
if (new_parent)
ReorderLayers();
UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL));
SchedulePaint();
Widget* widget = GetWidget();
if (widget)
widget->UpdateRootLayers();
}
bool View::ProcessMousePressed(const ui::MouseEvent& event) {
int drag_operations =
(enabled_ && event.IsOnlyLeftMouseButton() &&
HitTestPoint(event.location())) ?
GetDragOperations(event.location()) : 0;
ContextMenuController* context_menu_controller = event.IsRightMouseButton() ?
context_menu_controller_ : 0;
View::DragInfo* drag_info = GetDragInfo();
const bool enabled = enabled_;
const bool result = OnMousePressed(event);
if (!enabled)
return result;
if (event.IsOnlyRightMouseButton() && context_menu_controller &&
kContextMenuOnMousePress) {
gfx::Point location(event.location());
if (HitTestPoint(location)) {
ConvertPointToScreen(this, &location);
ShowContextMenu(location, ui::MENU_SOURCE_MOUSE);
return true;
}
}
if (drag_operations != ui::DragDropTypes::DRAG_NONE) {
drag_info->PossibleDrag(event.location());
return true;
}
return !!context_menu_controller || result;
}
bool View::ProcessMouseDragged(const ui::MouseEvent& event) {
ContextMenuController* context_menu_controller = context_menu_controller_;
const bool possible_drag = GetDragInfo()->possible_drag;
if (possible_drag &&
ExceededDragThreshold(GetDragInfo()->start_pt - event.location()) &&
(!drag_controller_ ||
drag_controller_->CanStartDragForView(
this, GetDragInfo()->start_pt, event.location()))) {
DoDrag(event, GetDragInfo()->start_pt,
ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
} else {
if (OnMouseDragged(event))
return true;
}
return (context_menu_controller != NULL) || possible_drag;
}
void View::ProcessMouseReleased(const ui::MouseEvent& event) {
if (!kContextMenuOnMousePress && context_menu_controller_ &&
event.IsOnlyRightMouseButton()) {
gfx::Point location(event.location());
OnMouseReleased(event);
if (HitTestPoint(location)) {
ConvertPointToScreen(this, &location);
ShowContextMenu(location, ui::MENU_SOURCE_MOUSE);
}
} else {
OnMouseReleased(event);
}
}
void View::RegisterPendingAccelerators() {
if (!accelerators_.get() ||
registered_accelerator_count_ == accelerators_->size()) {
return;
}
if (!GetWidget()) {
return;
}
accelerator_focus_manager_ = GetFocusManager();
if (!accelerator_focus_manager_) {
NOTREACHED();
return;
}
for (std::vector<ui::Accelerator>::const_iterator i(
accelerators_->begin() + registered_accelerator_count_);
i != accelerators_->end(); ++i) {
accelerator_focus_manager_->RegisterAccelerator(
*i, ui::AcceleratorManager::kNormalPriority, this);
}
registered_accelerator_count_ = accelerators_->size();
}
void View::UnregisterAccelerators(bool leave_data_intact) {
if (!accelerators_.get())
return;
if (GetWidget()) {
if (accelerator_focus_manager_) {
accelerator_focus_manager_->UnregisterAccelerators(this);
accelerator_focus_manager_ = NULL;
}
if (!leave_data_intact) {
accelerators_->clear();
accelerators_.reset();
}
registered_accelerator_count_ = 0;
}
}
void View::InitFocusSiblings(View* v, int index) {
int count = child_count();
if (count == 0) {
v->next_focusable_view_ = NULL;
v->previous_focusable_view_ = NULL;
} else {
if (index == count) {
View* last_focusable_view = NULL;
for (Views::iterator i(children_.begin()); i != children_.end(); ++i) {
if (!(*i)->next_focusable_view_) {
last_focusable_view = *i;
break;
}
}
if (last_focusable_view == NULL) {
View* prev = children_[index - 1];
v->previous_focusable_view_ = prev;
v->next_focusable_view_ = prev->next_focusable_view_;
prev->next_focusable_view_->previous_focusable_view_ = v;
prev->next_focusable_view_ = v;
} else {
last_focusable_view->next_focusable_view_ = v;
v->next_focusable_view_ = NULL;
v->previous_focusable_view_ = last_focusable_view;
}
} else {
View* prev = children_[index]->GetPreviousFocusableView();
v->previous_focusable_view_ = prev;
v->next_focusable_view_ = children_[index];
if (prev)
prev->next_focusable_view_ = v;
children_[index]->previous_focusable_view_ = v;
}
}
}
void View::PropagateThemeChanged() {
for (int i = child_count() - 1; i >= 0; --i)
child_at(i)->PropagateThemeChanged();
OnThemeChanged();
}
void View::PropagateLocaleChanged() {
for (int i = child_count() - 1; i >= 0; --i)
child_at(i)->PropagateLocaleChanged();
OnLocaleChanged();
}
void View::UpdateTooltip() {
Widget* widget = GetWidget();
if (widget && widget->GetTooltipManager())
widget->GetTooltipManager()->UpdateTooltip();
}
bool View::DoDrag(const ui::LocatedEvent& event,
const gfx::Point& press_pt,
ui::DragDropTypes::DragEventSource source) {
int drag_operations = GetDragOperations(press_pt);
if (drag_operations == ui::DragDropTypes::DRAG_NONE)
return false;
Widget* widget = GetWidget();
DCHECK(widget);
if (widget->dragged_view())
return false;
OSExchangeData data;
WriteDragData(press_pt, &data);
gfx::Point widget_location(event.location());
ConvertPointToWidget(this, &widget_location);
widget->RunShellDrag(this, data, widget_location, drag_operations, source);
return true;
}
}