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 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first.
7 #include "ui/views/view.h"
12 #include "base/debug/trace_event.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "third_party/skia/include/core/SkRect.h"
19 #include "ui/accessibility/ax_enums.h"
20 #include "ui/base/cursor/cursor.h"
21 #include "ui/base/dragdrop/drag_drop_types.h"
22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/dip_util.h"
24 #include "ui/compositor/layer.h"
25 #include "ui/compositor/layer_animator.h"
26 #include "ui/events/event_target_iterator.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/interpolated_transform.h"
29 #include "ui/gfx/path.h"
30 #include "ui/gfx/point3_f.h"
31 #include "ui/gfx/point_conversions.h"
32 #include "ui/gfx/scoped_canvas.h"
33 #include "ui/gfx/screen.h"
34 #include "ui/gfx/skia_util.h"
35 #include "ui/gfx/transform.h"
36 #include "ui/native_theme/native_theme.h"
37 #include "ui/views/accessibility/native_view_accessibility.h"
38 #include "ui/views/background.h"
39 #include "ui/views/border.h"
40 #include "ui/views/context_menu_controller.h"
41 #include "ui/views/drag_controller.h"
42 #include "ui/views/focus/view_storage.h"
43 #include "ui/views/layout/layout_manager.h"
44 #include "ui/views/views_delegate.h"
45 #include "ui/views/widget/native_widget_private.h"
46 #include "ui/views/widget/root_view.h"
47 #include "ui/views/widget/tooltip_manager.h"
48 #include "ui/views/widget/widget.h"
51 #include "base/win/scoped_gdi_object.h"
57 const bool kContextMenuOnMousePress
= false;
59 const bool kContextMenuOnMousePress
= true;
62 // Default horizontal drag threshold in pixels.
63 // Same as what gtk uses.
64 const int kDefaultHorizontalDragThreshold
= 8;
66 // Default vertical drag threshold in pixels.
67 // Same as what gtk uses.
68 const int kDefaultVerticalDragThreshold
= 8;
70 // Returns the top view in |view|'s hierarchy.
71 const views::View
* GetHierarchyRoot(const views::View
* view
) {
72 const views::View
* root
= view
;
73 while (root
&& root
->parent())
74 root
= root
->parent();
84 } // namespace internal
87 ViewsDelegate
* ViewsDelegate::views_delegate
= NULL
;
90 const char View::kViewClassName
[] = "View";
92 ////////////////////////////////////////////////////////////////////////////////
95 // Creation and lifetime -------------------------------------------------------
98 : owned_by_client_(false),
104 notify_enter_exit_on_child_(false),
105 registered_for_visible_bounds_notification_(false),
106 root_bounds_dirty_(true),
107 clip_insets_(0, 0, 0, 0),
109 snap_layer_to_pixel_boundary_(false),
110 flip_canvas_on_paint_for_rtl_ui_(false),
111 paint_to_layer_(false),
112 accelerator_focus_manager_(NULL
),
113 registered_accelerator_count_(0),
114 next_focusable_view_(NULL
),
115 previous_focusable_view_(NULL
),
117 accessibility_focusable_(false),
118 context_menu_controller_(NULL
),
119 drag_controller_(NULL
),
120 native_view_accessibility_(NULL
) {
125 parent_
->RemoveChildView(this);
127 ViewStorage::GetInstance()->ViewRemoved(this);
129 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
130 (*i
)->parent_
= NULL
;
131 if (!(*i
)->owned_by_client_
)
135 // Release ownership of the native accessibility object, but it's
136 // reference-counted on some platforms, so it may not be deleted right away.
137 if (native_view_accessibility_
)
138 native_view_accessibility_
->Destroy();
141 // Tree operations -------------------------------------------------------------
143 const Widget
* View::GetWidget() const {
144 // The root view holds a reference to this view hierarchy's Widget.
145 return parent_
? parent_
->GetWidget() : NULL
;
148 Widget
* View::GetWidget() {
149 return const_cast<Widget
*>(const_cast<const View
*>(this)->GetWidget());
152 void View::AddChildView(View
* view
) {
153 if (view
->parent_
== this)
155 AddChildViewAt(view
, child_count());
158 void View::AddChildViewAt(View
* view
, int index
) {
159 CHECK_NE(view
, this) << "You cannot add a view as its own child";
161 DCHECK_LE(index
, child_count());
163 // If |view| has a parent, remove it from its parent.
164 View
* parent
= view
->parent_
;
165 ui::NativeTheme
* old_theme
= NULL
;
167 old_theme
= view
->GetNativeTheme();
168 if (parent
== this) {
169 ReorderChildView(view
, index
);
172 parent
->DoRemoveChildView(view
, true, true, false, this);
175 // Sets the prev/next focus views.
176 InitFocusSiblings(view
, index
);
178 // Let's insert the view.
179 view
->parent_
= this;
180 children_
.insert(children_
.begin() + index
, view
);
182 // Instruct the view to recompute its root bounds on next Paint().
183 view
->SetRootBoundsDirty(true);
185 views::Widget
* widget
= GetWidget();
187 const ui::NativeTheme
* new_theme
= view
->GetNativeTheme();
188 if (new_theme
!= old_theme
)
189 view
->PropagateNativeThemeChanged(new_theme
);
192 ViewHierarchyChangedDetails
details(true, this, view
, parent
);
194 for (View
* v
= this; v
; v
= v
->parent_
)
195 v
->ViewHierarchyChangedImpl(false, details
);
197 view
->PropagateAddNotifications(details
);
200 RegisterChildrenForVisibleBoundsNotification(view
);
202 view
->SchedulePaint();
205 if (layout_manager_
.get())
206 layout_manager_
->ViewAdded(this, view
);
210 // Make sure the visibility of the child layers are correct.
211 // If any of the parent View is hidden, then the layers of the subtree
212 // rooted at |this| should be hidden. Otherwise, all the child layers should
213 // inherit the visibility of the owner View.
214 UpdateLayerVisibility();
217 void View::ReorderChildView(View
* view
, int index
) {
218 DCHECK_EQ(view
->parent_
, this);
220 index
= child_count() - 1;
221 else if (index
>= child_count())
223 if (children_
[index
] == view
)
226 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
227 DCHECK(i
!= children_
.end());
230 // Unlink the view first
231 View
* next_focusable
= view
->next_focusable_view_
;
232 View
* prev_focusable
= view
->previous_focusable_view_
;
234 prev_focusable
->next_focusable_view_
= next_focusable
;
236 next_focusable
->previous_focusable_view_
= prev_focusable
;
238 // Add it in the specified index now.
239 InitFocusSiblings(view
, index
);
240 children_
.insert(children_
.begin() + index
, view
);
245 void View::RemoveChildView(View
* view
) {
246 DoRemoveChildView(view
, true, true, false, NULL
);
249 void View::RemoveAllChildViews(bool delete_children
) {
250 while (!children_
.empty())
251 DoRemoveChildView(children_
.front(), false, false, delete_children
, NULL
);
255 bool View::Contains(const View
* view
) const {
256 for (const View
* v
= view
; v
; v
= v
->parent_
) {
263 int View::GetIndexOf(const View
* view
) const {
264 Views::const_iterator
i(std::find(children_
.begin(), children_
.end(), view
));
265 return i
!= children_
.end() ? static_cast<int>(i
- children_
.begin()) : -1;
268 // Size and disposition --------------------------------------------------------
270 void View::SetBounds(int x
, int y
, int width
, int height
) {
271 SetBoundsRect(gfx::Rect(x
, y
, std::max(0, width
), std::max(0, height
)));
274 void View::SetBoundsRect(const gfx::Rect
& bounds
) {
275 if (bounds
== bounds_
) {
277 needs_layout_
= false;
284 // Paint where the view is currently.
285 SchedulePaintBoundsChanged(
286 bounds_
.size() == bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
287 SCHEDULE_PAINT_SIZE_CHANGED
);
290 gfx::Rect prev
= bounds_
;
295 void View::SetSize(const gfx::Size
& size
) {
296 SetBounds(x(), y(), size
.width(), size
.height());
299 void View::SetPosition(const gfx::Point
& position
) {
300 SetBounds(position
.x(), position
.y(), width(), height());
303 void View::SetX(int x
) {
304 SetBounds(x
, y(), width(), height());
307 void View::SetY(int y
) {
308 SetBounds(x(), y
, width(), height());
311 gfx::Rect
View::GetContentsBounds() const {
312 gfx::Rect
contents_bounds(GetLocalBounds());
314 contents_bounds
.Inset(border_
->GetInsets());
315 return contents_bounds
;
318 gfx::Rect
View::GetLocalBounds() const {
319 return gfx::Rect(size());
322 gfx::Rect
View::GetLayerBoundsInPixel() const {
323 return layer()->GetTargetBounds();
326 gfx::Insets
View::GetInsets() const {
327 return border_
.get() ? border_
->GetInsets() : gfx::Insets();
330 gfx::Rect
View::GetVisibleBounds() const {
333 gfx::Rect
vis_bounds(GetLocalBounds());
334 gfx::Rect ancestor_bounds
;
335 const View
* view
= this;
336 gfx::Transform transform
;
338 while (view
!= NULL
&& !vis_bounds
.IsEmpty()) {
339 transform
.ConcatTransform(view
->GetTransform());
340 gfx::Transform translation
;
341 translation
.Translate(static_cast<float>(view
->GetMirroredX()),
342 static_cast<float>(view
->y()));
343 transform
.ConcatTransform(translation
);
345 vis_bounds
= view
->ConvertRectToParent(vis_bounds
);
346 const View
* ancestor
= view
->parent_
;
347 if (ancestor
!= NULL
) {
348 ancestor_bounds
.SetRect(0, 0, ancestor
->width(), ancestor
->height());
349 vis_bounds
.Intersect(ancestor_bounds
);
350 } else if (!view
->GetWidget()) {
351 // If the view has no Widget, we're not visible. Return an empty rect.
356 if (vis_bounds
.IsEmpty())
358 // Convert back to this views coordinate system.
359 gfx::RectF
views_vis_bounds(vis_bounds
);
360 transform
.TransformRectReverse(&views_vis_bounds
);
361 // Partially visible pixels should be considered visible.
362 return gfx::ToEnclosingRect(views_vis_bounds
);
365 gfx::Rect
View::GetBoundsInScreen() const {
367 View::ConvertPointToScreen(this, &origin
);
368 return gfx::Rect(origin
, size());
371 gfx::Size
View::GetPreferredSize() const {
372 if (layout_manager_
.get())
373 return layout_manager_
->GetPreferredSize(this);
377 int View::GetBaseline() const {
381 void View::SizeToPreferredSize() {
382 gfx::Size prefsize
= GetPreferredSize();
383 if ((prefsize
.width() != width()) || (prefsize
.height() != height()))
384 SetBounds(x(), y(), prefsize
.width(), prefsize
.height());
387 gfx::Size
View::GetMinimumSize() const {
388 return GetPreferredSize();
391 gfx::Size
View::GetMaximumSize() const {
395 int View::GetHeightForWidth(int w
) const {
396 if (layout_manager_
.get())
397 return layout_manager_
->GetPreferredHeightForWidth(this, w
);
398 return GetPreferredSize().height();
401 void View::SetVisible(bool visible
) {
402 if (visible
!= visible_
) {
403 // If the View is currently visible, schedule paint to refresh parent.
404 // TODO(beng): not sure we should be doing this if we have a layer.
409 AdvanceFocusIfNecessary();
411 // Notify the parent.
413 parent_
->ChildVisibilityChanged(this);
415 // This notifies all sub-views recursively.
416 PropagateVisibilityNotifications(this, visible_
);
417 UpdateLayerVisibility();
419 // If we are newly visible, schedule paint.
423 // We're never painted when hidden, so no need to be in the BoundsTree.
424 BoundsTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
426 RemoveRootBounds(bounds_tree
);
431 bool View::IsDrawn() const {
432 return visible_
&& parent_
? parent_
->IsDrawn() : false;
435 void View::SetEnabled(bool enabled
) {
436 if (enabled
!= enabled_
) {
438 AdvanceFocusIfNecessary();
443 void View::OnEnabledChanged() {
447 // Transformations -------------------------------------------------------------
449 gfx::Transform
View::GetTransform() const {
450 return layer() ? layer()->transform() : gfx::Transform();
453 void View::SetTransform(const gfx::Transform
& transform
) {
454 if (transform
.IsIdentity()) {
456 layer()->SetTransform(transform
);
457 if (!paint_to_layer_
)
465 layer()->SetTransform(transform
);
466 layer()->ScheduleDraw();
470 void View::SetPaintToLayer(bool paint_to_layer
) {
471 if (paint_to_layer_
== paint_to_layer
)
474 // If this is a change in state we will also need to update bounds trees.
475 if (paint_to_layer
) {
476 // Gaining a layer means becoming a paint root. We must remove ourselves
477 // from our old paint root, if we had one. Traverse up view tree to find old
479 View
* old_paint_root
= parent_
;
480 while (old_paint_root
&& !old_paint_root
->IsPaintRoot())
481 old_paint_root
= old_paint_root
->parent_
;
483 // Remove our and our children's bounds from the old tree. This will also
484 // mark all of our bounds as dirty.
485 if (old_paint_root
&& old_paint_root
->bounds_tree_
)
486 RemoveRootBounds(old_paint_root
->bounds_tree_
.get());
489 // Losing a layer means we are no longer a paint root, so delete our
490 // bounds tree and mark ourselves as dirty for future insertion into our
491 // new paint root's bounds tree.
492 bounds_tree_
.reset();
493 SetRootBoundsDirty(true);
496 paint_to_layer_
= paint_to_layer
;
497 if (paint_to_layer_
&& !layer()) {
499 } else if (!paint_to_layer_
&& layer()) {
504 // RTL positioning -------------------------------------------------------------
506 gfx::Rect
View::GetMirroredBounds() const {
507 gfx::Rect
bounds(bounds_
);
508 bounds
.set_x(GetMirroredX());
512 gfx::Point
View::GetMirroredPosition() const {
513 return gfx::Point(GetMirroredX(), y());
516 int View::GetMirroredX() const {
517 return parent_
? parent_
->GetMirroredXForRect(bounds_
) : x();
520 int View::GetMirroredXForRect(const gfx::Rect
& bounds
) const {
521 return base::i18n::IsRTL() ?
522 (width() - bounds
.x() - bounds
.width()) : bounds
.x();
525 int View::GetMirroredXInView(int x
) const {
526 return base::i18n::IsRTL() ? width() - x
: x
;
529 int View::GetMirroredXWithWidthInView(int x
, int w
) const {
530 return base::i18n::IsRTL() ? width() - x
- w
: x
;
533 // Layout ----------------------------------------------------------------------
535 void View::Layout() {
536 needs_layout_
= false;
538 // If we have a layout manager, let it handle the layout for us.
539 if (layout_manager_
.get())
540 layout_manager_
->Layout(this);
542 // Make sure to propagate the Layout() call to any children that haven't
543 // received it yet through the layout manager and need to be laid out. This
544 // is needed for the case when the child requires a layout but its bounds
545 // weren't changed by the layout manager. If there is no layout manager, we
546 // just propagate the Layout() call down the hierarchy, so whoever receives
547 // the call can take appropriate action.
548 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
549 View
* child
= child_at(i
);
550 if (child
->needs_layout_
|| !layout_manager_
.get()) {
551 child
->needs_layout_
= false;
557 void View::InvalidateLayout() {
558 // Always invalidate up. This is needed to handle the case of us already being
559 // valid, but not our parent.
560 needs_layout_
= true;
562 parent_
->InvalidateLayout();
565 LayoutManager
* View::GetLayoutManager() const {
566 return layout_manager_
.get();
569 void View::SetLayoutManager(LayoutManager
* layout_manager
) {
570 if (layout_manager_
.get())
571 layout_manager_
->Uninstalled(this);
573 layout_manager_
.reset(layout_manager
);
574 if (layout_manager_
.get())
575 layout_manager_
->Installed(this);
578 void View::SnapLayerToPixelBoundary() {
582 if (snap_layer_to_pixel_boundary_
&& layer()->parent() &&
583 layer()->GetCompositor()) {
584 ui::SnapLayerToPhysicalPixelBoundary(layer()->parent(), layer());
587 layer()->SetSubpixelPositionOffset(gfx::Vector2dF());
591 // Attributes ------------------------------------------------------------------
593 const char* View::GetClassName() const {
594 return kViewClassName
;
597 const View
* View::GetAncestorWithClassName(const std::string
& name
) const {
598 for (const View
* view
= this; view
; view
= view
->parent_
) {
599 if (!strcmp(view
->GetClassName(), name
.c_str()))
605 View
* View::GetAncestorWithClassName(const std::string
& name
) {
606 return const_cast<View
*>(const_cast<const View
*>(this)->
607 GetAncestorWithClassName(name
));
610 const View
* View::GetViewByID(int id
) const {
612 return const_cast<View
*>(this);
614 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
615 const View
* view
= child_at(i
)->GetViewByID(id
);
622 View
* View::GetViewByID(int id
) {
623 return const_cast<View
*>(const_cast<const View
*>(this)->GetViewByID(id
));
626 void View::SetGroup(int gid
) {
627 // Don't change the group id once it's set.
628 DCHECK(group_
== -1 || group_
== gid
);
632 int View::GetGroup() const {
636 bool View::IsGroupFocusTraversable() const {
640 void View::GetViewsInGroup(int group
, Views
* views
) {
642 views
->push_back(this);
644 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
645 child_at(i
)->GetViewsInGroup(group
, views
);
648 View
* View::GetSelectedViewForGroup(int group
) {
650 GetWidget()->GetRootView()->GetViewsInGroup(group
, &views
);
651 return views
.empty() ? NULL
: views
[0];
654 // Coordinate conversion -------------------------------------------------------
657 void View::ConvertPointToTarget(const View
* source
,
662 if (source
== target
)
665 const View
* root
= GetHierarchyRoot(target
);
666 CHECK_EQ(GetHierarchyRoot(source
), root
);
669 source
->ConvertPointForAncestor(root
, point
);
672 target
->ConvertPointFromAncestor(root
, point
);
676 void View::ConvertRectToTarget(const View
* source
,
681 if (source
== target
)
684 const View
* root
= GetHierarchyRoot(target
);
685 CHECK_EQ(GetHierarchyRoot(source
), root
);
688 source
->ConvertRectForAncestor(root
, rect
);
691 target
->ConvertRectFromAncestor(root
, rect
);
695 void View::ConvertPointToWidget(const View
* src
, gfx::Point
* p
) {
699 src
->ConvertPointForAncestor(NULL
, p
);
703 void View::ConvertPointFromWidget(const View
* dest
, gfx::Point
* p
) {
707 dest
->ConvertPointFromAncestor(NULL
, p
);
711 void View::ConvertPointToScreen(const View
* src
, gfx::Point
* p
) {
715 // If the view is not connected to a tree, there's nothing we can do.
716 const Widget
* widget
= src
->GetWidget();
718 ConvertPointToWidget(src
, p
);
719 *p
+= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
724 void View::ConvertPointFromScreen(const View
* dst
, gfx::Point
* p
) {
728 const views::Widget
* widget
= dst
->GetWidget();
731 *p
-= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
732 views::View::ConvertPointFromWidget(dst
, p
);
735 gfx::Rect
View::ConvertRectToParent(const gfx::Rect
& rect
) const {
736 gfx::RectF x_rect
= rect
;
737 GetTransform().TransformRect(&x_rect
);
738 x_rect
.Offset(GetMirroredPosition().OffsetFromOrigin());
739 // Pixels we partially occupy in the parent should be included.
740 return gfx::ToEnclosingRect(x_rect
);
743 gfx::Rect
View::ConvertRectToWidget(const gfx::Rect
& rect
) const {
744 gfx::Rect x_rect
= rect
;
745 for (const View
* v
= this; v
; v
= v
->parent_
)
746 x_rect
= v
->ConvertRectToParent(x_rect
);
750 // Painting --------------------------------------------------------------------
752 void View::SchedulePaint() {
753 SchedulePaintInRect(GetLocalBounds());
756 void View::SchedulePaintInRect(const gfx::Rect
& rect
) {
761 layer()->SchedulePaint(rect
);
762 } else if (parent_
) {
763 // Translate the requested paint rect to the parent's coordinate system
764 // then pass this notification up to the parent.
765 parent_
->SchedulePaintInRect(ConvertRectToParent(rect
));
769 void View::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
770 // The cull_set may allow us to skip painting without canvas construction or
771 // even canvas rect intersection.
772 if (cull_set
.ShouldPaint(this)) {
773 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
775 gfx::ScopedCanvas
scoped_canvas(canvas
);
777 // Paint this View and its children, setting the clip rect to the bounds
778 // of this View and translating the origin to the local bounds' top left
781 // Note that the X (or left) position we pass to ClipRectInt takes into
782 // consideration whether or not the view uses a right-to-left layout so that
783 // we paint our view in its mirrored position if need be.
784 gfx::Rect clip_rect
= bounds();
785 clip_rect
.Inset(clip_insets_
);
787 clip_rect
.set_x(parent_
->GetMirroredXForRect(clip_rect
));
788 canvas
->ClipRect(clip_rect
);
789 if (canvas
->IsClipEmpty())
792 // Non-empty clip, translate the graphics such that 0,0 corresponds to where
793 // this view is located (related to its parent).
794 canvas
->Translate(GetMirroredPosition().OffsetFromOrigin());
795 canvas
->Transform(GetTransform());
797 // If we are a paint root, we need to construct our own CullSet object for
798 // propagation to our children.
801 bounds_tree_
.reset(new BoundsTree(2, 5));
803 // Recompute our bounds tree as needed.
804 UpdateRootBounds(bounds_tree_
.get(), gfx::Vector2d());
806 // Grab the clip rect from the supplied canvas to use as the query rect.
807 gfx::Rect canvas_bounds
;
808 if (!canvas
->GetClipBounds(&canvas_bounds
)) {
809 NOTREACHED() << "Failed to get clip bounds from the canvas!";
813 // Now query our bounds_tree_ for a set of damaged views that intersect
814 // our canvas bounds.
815 scoped_ptr
<base::hash_set
<intptr_t> > damaged_views(
816 new base::hash_set
<intptr_t>());
817 bounds_tree_
->AppendIntersectingRecords(
818 canvas_bounds
, damaged_views
.get());
819 // Construct a CullSet to wrap the damaged views set, it will delete it
820 // for us on scope exit.
821 CullSet
paint_root_cull_set(damaged_views
.Pass());
822 // Paint all descendents using our new cull set.
823 PaintCommon(canvas
, paint_root_cull_set
);
825 // Not a paint root, so we can proceed as normal.
826 PaintCommon(canvas
, cull_set
);
831 void View::set_background(Background
* b
) {
832 background_
.reset(b
);
835 void View::SetBorder(scoped_ptr
<Border
> b
) { border_
= b
.Pass(); }
837 ui::ThemeProvider
* View::GetThemeProvider() const {
838 const Widget
* widget
= GetWidget();
839 return widget
? widget
->GetThemeProvider() : NULL
;
842 const ui::NativeTheme
* View::GetNativeTheme() const {
843 const Widget
* widget
= GetWidget();
844 return widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
847 // Input -----------------------------------------------------------------------
849 View
* View::GetEventHandlerForPoint(const gfx::Point
& point
) {
850 return GetEventHandlerForRect(gfx::Rect(point
, gfx::Size(1, 1)));
853 View
* View::GetEventHandlerForRect(const gfx::Rect
& rect
) {
854 return GetEffectiveViewTargeter()->TargetForRect(this, rect
);
857 bool View::CanProcessEventsWithinSubtree() const {
861 View
* View::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
862 // TODO(tdanderson): Move this implementation into ViewTargetDelegate.
863 if (!HitTestPoint(point
) || !CanProcessEventsWithinSubtree())
866 // Walk the child Views recursively looking for the View that most
867 // tightly encloses the specified point.
868 for (int i
= child_count() - 1; i
>= 0; --i
) {
869 View
* child
= child_at(i
);
870 if (!child
->visible())
873 gfx::Point
point_in_child_coords(point
);
874 ConvertPointToTarget(this, child
, &point_in_child_coords
);
875 View
* handler
= child
->GetTooltipHandlerForPoint(point_in_child_coords
);
882 gfx::NativeCursor
View::GetCursor(const ui::MouseEvent
& event
) {
884 static ui::Cursor arrow
;
885 if (!arrow
.platform())
886 arrow
.SetPlatformCursor(LoadCursor(NULL
, IDC_ARROW
));
889 return gfx::kNullCursor
;
893 bool View::HitTestPoint(const gfx::Point
& point
) const {
894 return HitTestRect(gfx::Rect(point
, gfx::Size(1, 1)));
897 bool View::HitTestRect(const gfx::Rect
& rect
) const {
898 return GetEffectiveViewTargeter()->DoesIntersectRect(this, rect
);
901 bool View::IsMouseHovered() {
902 // If we haven't yet been placed in an onscreen view hierarchy, we can't be
907 // If mouse events are disabled, then the mouse cursor is invisible and
908 // is therefore not hovering over this button.
909 if (!GetWidget()->IsMouseEventsEnabled())
912 gfx::Point
cursor_pos(gfx::Screen::GetScreenFor(
913 GetWidget()->GetNativeView())->GetCursorScreenPoint());
914 ConvertPointFromScreen(this, &cursor_pos
);
915 return HitTestPoint(cursor_pos
);
918 bool View::OnMousePressed(const ui::MouseEvent
& event
) {
922 bool View::OnMouseDragged(const ui::MouseEvent
& event
) {
926 void View::OnMouseReleased(const ui::MouseEvent
& event
) {
929 void View::OnMouseCaptureLost() {
932 void View::OnMouseMoved(const ui::MouseEvent
& event
) {
935 void View::OnMouseEntered(const ui::MouseEvent
& event
) {
938 void View::OnMouseExited(const ui::MouseEvent
& event
) {
941 void View::SetMouseHandler(View
* new_mouse_handler
) {
942 // |new_mouse_handler| may be NULL.
944 parent_
->SetMouseHandler(new_mouse_handler
);
947 bool View::OnKeyPressed(const ui::KeyEvent
& event
) {
951 bool View::OnKeyReleased(const ui::KeyEvent
& event
) {
955 bool View::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
959 void View::OnKeyEvent(ui::KeyEvent
* event
) {
960 bool consumed
= (event
->type() == ui::ET_KEY_PRESSED
) ? OnKeyPressed(*event
) :
961 OnKeyReleased(*event
);
963 event
->StopPropagation();
966 void View::OnMouseEvent(ui::MouseEvent
* event
) {
967 switch (event
->type()) {
968 case ui::ET_MOUSE_PRESSED
:
969 if (ProcessMousePressed(*event
))
973 case ui::ET_MOUSE_MOVED
:
974 CHECK(!event
->IsAnyButton());
975 OnMouseMoved(*event
);
978 case ui::ET_MOUSE_DRAGGED
:
979 if (ProcessMouseDragged(*event
))
983 case ui::ET_MOUSE_RELEASED
:
984 ProcessMouseReleased(*event
);
987 case ui::ET_MOUSEWHEEL
:
988 if (OnMouseWheel(*static_cast<ui::MouseWheelEvent
*>(event
)))
992 case ui::ET_MOUSE_ENTERED
:
993 if (event
->flags() & ui::EF_TOUCH_ACCESSIBILITY
)
994 NotifyAccessibilityEvent(ui::AX_EVENT_HOVER
, true);
995 OnMouseEntered(*event
);
998 case ui::ET_MOUSE_EXITED
:
999 OnMouseExited(*event
);
1007 void View::OnScrollEvent(ui::ScrollEvent
* event
) {
1010 void View::OnTouchEvent(ui::TouchEvent
* event
) {
1011 NOTREACHED() << "Views should not receive touch events.";
1014 void View::OnGestureEvent(ui::GestureEvent
* event
) {
1017 ui::TextInputClient
* View::GetTextInputClient() {
1021 InputMethod
* View::GetInputMethod() {
1022 Widget
* widget
= GetWidget();
1023 return widget
? widget
->GetInputMethod() : NULL
;
1026 const InputMethod
* View::GetInputMethod() const {
1027 const Widget
* widget
= GetWidget();
1028 return widget
? widget
->GetInputMethod() : NULL
;
1031 scoped_ptr
<ViewTargeter
>
1032 View::SetEventTargeter(scoped_ptr
<ViewTargeter
> targeter
) {
1033 scoped_ptr
<ViewTargeter
> old_targeter
= targeter_
.Pass();
1034 targeter_
= targeter
.Pass();
1035 return old_targeter
.Pass();
1038 ViewTargeter
* View::GetEffectiveViewTargeter() const {
1039 DCHECK(GetWidget());
1040 ViewTargeter
* view_targeter
= targeter();
1042 view_targeter
= GetWidget()->GetRootView()->targeter();
1043 CHECK(view_targeter
);
1044 return view_targeter
;
1047 bool View::CanAcceptEvent(const ui::Event
& event
) {
1051 ui::EventTarget
* View::GetParentTarget() {
1055 scoped_ptr
<ui::EventTargetIterator
> View::GetChildIterator() const {
1056 return scoped_ptr
<ui::EventTargetIterator
>(
1057 new ui::EventTargetIteratorImpl
<View
>(children_
));
1060 ui::EventTargeter
* View::GetEventTargeter() {
1061 return targeter_
.get();
1064 void View::ConvertEventToTarget(ui::EventTarget
* target
,
1065 ui::LocatedEvent
* event
) {
1066 event
->ConvertLocationToTarget(this, static_cast<View
*>(target
));
1069 // Accelerators ----------------------------------------------------------------
1071 void View::AddAccelerator(const ui::Accelerator
& accelerator
) {
1072 if (!accelerators_
.get())
1073 accelerators_
.reset(new std::vector
<ui::Accelerator
>());
1075 if (std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
) ==
1076 accelerators_
->end()) {
1077 accelerators_
->push_back(accelerator
);
1079 RegisterPendingAccelerators();
1082 void View::RemoveAccelerator(const ui::Accelerator
& accelerator
) {
1083 if (!accelerators_
.get()) {
1084 NOTREACHED() << "Removing non-existing accelerator";
1088 std::vector
<ui::Accelerator
>::iterator
i(
1089 std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
));
1090 if (i
== accelerators_
->end()) {
1091 NOTREACHED() << "Removing non-existing accelerator";
1095 size_t index
= i
- accelerators_
->begin();
1096 accelerators_
->erase(i
);
1097 if (index
>= registered_accelerator_count_
) {
1098 // The accelerator is not registered to FocusManager.
1101 --registered_accelerator_count_
;
1103 // Providing we are attached to a Widget and registered with a focus manager,
1104 // we should de-register from that focus manager now.
1105 if (GetWidget() && accelerator_focus_manager_
)
1106 accelerator_focus_manager_
->UnregisterAccelerator(accelerator
, this);
1109 void View::ResetAccelerators() {
1110 if (accelerators_
.get())
1111 UnregisterAccelerators(false);
1114 bool View::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1118 bool View::CanHandleAccelerators() const {
1119 return enabled() && IsDrawn() && GetWidget() && GetWidget()->IsVisible();
1122 // Focus -----------------------------------------------------------------------
1124 bool View::HasFocus() const {
1125 const FocusManager
* focus_manager
= GetFocusManager();
1126 return focus_manager
&& (focus_manager
->GetFocusedView() == this);
1129 View
* View::GetNextFocusableView() {
1130 return next_focusable_view_
;
1133 const View
* View::GetNextFocusableView() const {
1134 return next_focusable_view_
;
1137 View
* View::GetPreviousFocusableView() {
1138 return previous_focusable_view_
;
1141 void View::SetNextFocusableView(View
* view
) {
1143 view
->previous_focusable_view_
= this;
1144 next_focusable_view_
= view
;
1147 void View::SetFocusable(bool focusable
) {
1148 if (focusable_
== focusable
)
1151 focusable_
= focusable
;
1152 AdvanceFocusIfNecessary();
1155 bool View::IsFocusable() const {
1156 return focusable_
&& enabled_
&& IsDrawn();
1159 bool View::IsAccessibilityFocusable() const {
1160 return (focusable_
|| accessibility_focusable_
) && enabled_
&& IsDrawn();
1163 void View::SetAccessibilityFocusable(bool accessibility_focusable
) {
1164 if (accessibility_focusable_
== accessibility_focusable
)
1167 accessibility_focusable_
= accessibility_focusable
;
1168 AdvanceFocusIfNecessary();
1171 FocusManager
* View::GetFocusManager() {
1172 Widget
* widget
= GetWidget();
1173 return widget
? widget
->GetFocusManager() : NULL
;
1176 const FocusManager
* View::GetFocusManager() const {
1177 const Widget
* widget
= GetWidget();
1178 return widget
? widget
->GetFocusManager() : NULL
;
1181 void View::RequestFocus() {
1182 FocusManager
* focus_manager
= GetFocusManager();
1183 if (focus_manager
&& IsFocusable())
1184 focus_manager
->SetFocusedView(this);
1187 bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) {
1191 FocusTraversable
* View::GetFocusTraversable() {
1195 FocusTraversable
* View::GetPaneFocusTraversable() {
1199 // Tooltips --------------------------------------------------------------------
1201 bool View::GetTooltipText(const gfx::Point
& p
, base::string16
* tooltip
) const {
1205 bool View::GetTooltipTextOrigin(const gfx::Point
& p
, gfx::Point
* loc
) const {
1209 // Context menus ---------------------------------------------------------------
1211 void View::ShowContextMenu(const gfx::Point
& p
,
1212 ui::MenuSourceType source_type
) {
1213 if (!context_menu_controller_
)
1216 context_menu_controller_
->ShowContextMenuForView(this, p
, source_type
);
1220 bool View::ShouldShowContextMenuOnMousePress() {
1221 return kContextMenuOnMousePress
;
1224 // Drag and drop ---------------------------------------------------------------
1226 bool View::GetDropFormats(
1228 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
1232 bool View::AreDropTypesRequired() {
1236 bool View::CanDrop(const OSExchangeData
& data
) {
1237 // TODO(sky): when I finish up migration, this should default to true.
1241 void View::OnDragEntered(const ui::DropTargetEvent
& event
) {
1244 int View::OnDragUpdated(const ui::DropTargetEvent
& event
) {
1245 return ui::DragDropTypes::DRAG_NONE
;
1248 void View::OnDragExited() {
1251 int View::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1252 return ui::DragDropTypes::DRAG_NONE
;
1255 void View::OnDragDone() {
1259 bool View::ExceededDragThreshold(const gfx::Vector2d
& delta
) {
1260 return (abs(delta
.x()) > GetHorizontalDragThreshold() ||
1261 abs(delta
.y()) > GetVerticalDragThreshold());
1264 // Accessibility----------------------------------------------------------------
1266 gfx::NativeViewAccessible
View::GetNativeViewAccessible() {
1267 if (!native_view_accessibility_
)
1268 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1269 if (native_view_accessibility_
)
1270 return native_view_accessibility_
->GetNativeObject();
1274 void View::NotifyAccessibilityEvent(
1275 ui::AXEvent event_type
,
1276 bool send_native_event
) {
1277 if (ViewsDelegate::views_delegate
)
1278 ViewsDelegate::views_delegate
->NotifyAccessibilityEvent(this, event_type
);
1280 if (send_native_event
&& GetWidget()) {
1281 if (!native_view_accessibility_
)
1282 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1283 if (native_view_accessibility_
)
1284 native_view_accessibility_
->NotifyAccessibilityEvent(event_type
);
1288 // Scrolling -------------------------------------------------------------------
1290 void View::ScrollRectToVisible(const gfx::Rect
& rect
) {
1291 // We must take RTL UI mirroring into account when adjusting the position of
1294 gfx::Rect
scroll_rect(rect
);
1295 scroll_rect
.Offset(GetMirroredX(), y());
1296 parent_
->ScrollRectToVisible(scroll_rect
);
1300 int View::GetPageScrollIncrement(ScrollView
* scroll_view
,
1301 bool is_horizontal
, bool is_positive
) {
1305 int View::GetLineScrollIncrement(ScrollView
* scroll_view
,
1306 bool is_horizontal
, bool is_positive
) {
1310 ////////////////////////////////////////////////////////////////////////////////
1313 // Size and disposition --------------------------------------------------------
1315 void View::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1318 void View::PreferredSizeChanged() {
1321 parent_
->ChildPreferredSizeChanged(this);
1324 bool View::GetNeedsNotificationWhenVisibleBoundsChange() const {
1328 void View::OnVisibleBoundsChanged() {
1331 // Tree operations -------------------------------------------------------------
1333 void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails
& details
) {
1336 void View::VisibilityChanged(View
* starting_from
, bool is_visible
) {
1339 void View::NativeViewHierarchyChanged() {
1340 FocusManager
* focus_manager
= GetFocusManager();
1341 if (accelerator_focus_manager_
!= focus_manager
) {
1342 UnregisterAccelerators(true);
1345 RegisterPendingAccelerators();
1349 // Painting --------------------------------------------------------------------
1351 void View::PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1352 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1353 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1354 if (!child_at(i
)->layer())
1355 child_at(i
)->Paint(canvas
, cull_set
);
1358 void View::OnPaint(gfx::Canvas
* canvas
) {
1359 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1360 OnPaintBackground(canvas
);
1361 OnPaintBorder(canvas
);
1364 void View::OnPaintBackground(gfx::Canvas
* canvas
) {
1365 if (background_
.get()) {
1366 TRACE_EVENT2("views", "View::OnPaintBackground",
1367 "width", canvas
->sk_canvas()->getDevice()->width(),
1368 "height", canvas
->sk_canvas()->getDevice()->height());
1369 background_
->Paint(canvas
, this);
1373 void View::OnPaintBorder(gfx::Canvas
* canvas
) {
1374 if (border_
.get()) {
1375 TRACE_EVENT2("views", "View::OnPaintBorder",
1376 "width", canvas
->sk_canvas()->getDevice()->width(),
1377 "height", canvas
->sk_canvas()->getDevice()->height());
1378 border_
->Paint(*this, canvas
);
1382 bool View::IsPaintRoot() {
1383 return paint_to_layer_
|| !parent_
;
1386 // Accelerated Painting --------------------------------------------------------
1388 void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely
) {
1389 // This method should not have the side-effect of creating the layer.
1391 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely
);
1394 gfx::Vector2d
View::CalculateOffsetToAncestorWithLayer(
1395 ui::Layer
** layer_parent
) {
1398 *layer_parent
= layer();
1399 return gfx::Vector2d();
1402 return gfx::Vector2d();
1404 return gfx::Vector2d(GetMirroredX(), y()) +
1405 parent_
->CalculateOffsetToAncestorWithLayer(layer_parent
);
1408 void View::UpdateParentLayer() {
1412 ui::Layer
* parent_layer
= NULL
;
1413 gfx::Vector2d
offset(GetMirroredX(), y());
1416 offset
+= parent_
->CalculateOffsetToAncestorWithLayer(&parent_layer
);
1418 ReparentLayer(offset
, parent_layer
);
1421 void View::MoveLayerToParent(ui::Layer
* parent_layer
,
1422 const gfx::Point
& point
) {
1423 gfx::Point
local_point(point
);
1424 if (parent_layer
!= layer())
1425 local_point
.Offset(GetMirroredX(), y());
1426 if (layer() && parent_layer
!= layer()) {
1427 parent_layer
->Add(layer());
1428 SetLayerBounds(gfx::Rect(local_point
.x(), local_point
.y(),
1429 width(), height()));
1431 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1432 child_at(i
)->MoveLayerToParent(parent_layer
, local_point
);
1436 void View::UpdateLayerVisibility() {
1437 bool visible
= visible_
;
1438 for (const View
* v
= parent_
; visible
&& v
&& !v
->layer(); v
= v
->parent_
)
1439 visible
= v
->visible();
1441 UpdateChildLayerVisibility(visible
);
1444 void View::UpdateChildLayerVisibility(bool ancestor_visible
) {
1446 layer()->SetVisible(ancestor_visible
&& visible_
);
1448 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1449 child_at(i
)->UpdateChildLayerVisibility(ancestor_visible
&& visible_
);
1453 void View::UpdateChildLayerBounds(const gfx::Vector2d
& offset
) {
1455 SetLayerBounds(GetLocalBounds() + offset
);
1457 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
1458 View
* child
= child_at(i
);
1459 child
->UpdateChildLayerBounds(
1460 offset
+ gfx::Vector2d(child
->GetMirroredX(), child
->y()));
1465 void View::OnPaintLayer(gfx::Canvas
* canvas
) {
1466 if (!layer() || !layer()->fills_bounds_opaquely())
1467 canvas
->DrawColor(SK_ColorBLACK
, SkXfermode::kClear_Mode
);
1468 PaintCommon(canvas
, CullSet());
1471 void View::OnDelegatedFrameDamage(
1472 const gfx::Rect
& damage_rect_in_dip
) {
1475 void View::OnDeviceScaleFactorChanged(float device_scale_factor
) {
1476 snap_layer_to_pixel_boundary_
=
1477 (device_scale_factor
- std::floor(device_scale_factor
)) != 0.0f
;
1478 SnapLayerToPixelBoundary();
1479 // Repainting with new scale factor will paint the content at the right scale.
1482 base::Closure
View::PrepareForLayerBoundsChange() {
1483 return base::Closure();
1486 void View::ReorderLayers() {
1488 while (v
&& !v
->layer())
1491 Widget
* widget
= GetWidget();
1494 ui::Layer
* layer
= widget
->GetLayer();
1496 widget
->GetRootView()->ReorderChildLayers(layer
);
1499 v
->ReorderChildLayers(v
->layer());
1503 // Reorder the widget's child NativeViews in case a child NativeView is
1504 // associated with a view (eg via a NativeViewHost). Always do the
1505 // reordering because the associated NativeView's layer (if it has one)
1506 // is parented to the widget's layer regardless of whether the host view has
1507 // an ancestor with a layer.
1508 widget
->ReorderNativeViews();
1512 void View::ReorderChildLayers(ui::Layer
* parent_layer
) {
1513 if (layer() && layer() != parent_layer
) {
1514 DCHECK_EQ(parent_layer
, layer()->parent());
1515 parent_layer
->StackAtBottom(layer());
1517 // Iterate backwards through the children so that a child with a layer
1518 // which is further to the back is stacked above one which is further to
1520 for (Views::reverse_iterator
it(children_
.rbegin());
1521 it
!= children_
.rend(); ++it
) {
1522 (*it
)->ReorderChildLayers(parent_layer
);
1527 // Input -----------------------------------------------------------------------
1529 View::DragInfo
* View::GetDragInfo() {
1530 return parent_
? parent_
->GetDragInfo() : NULL
;
1533 // Focus -----------------------------------------------------------------------
1535 void View::OnFocus() {
1536 // TODO(beng): Investigate whether it's possible for us to move this to
1538 // By default, we clear the native focus. This ensures that no visible native
1539 // view as the focus and that we still receive keyboard inputs.
1540 FocusManager
* focus_manager
= GetFocusManager();
1542 focus_manager
->ClearNativeFocus();
1544 // TODO(beng): Investigate whether it's possible for us to move this to
1546 // Notify assistive technologies of the focus change.
1547 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
1550 void View::OnBlur() {
1553 void View::Focus() {
1561 // Tooltips --------------------------------------------------------------------
1563 void View::TooltipTextChanged() {
1564 Widget
* widget
= GetWidget();
1565 // TooltipManager may be null if there is a problem creating it.
1566 if (widget
&& widget
->GetTooltipManager())
1567 widget
->GetTooltipManager()->TooltipTextChanged(this);
1570 // Context menus ---------------------------------------------------------------
1572 gfx::Point
View::GetKeyboardContextMenuLocation() {
1573 gfx::Rect vis_bounds
= GetVisibleBounds();
1574 gfx::Point
screen_point(vis_bounds
.x() + vis_bounds
.width() / 2,
1575 vis_bounds
.y() + vis_bounds
.height() / 2);
1576 ConvertPointToScreen(this, &screen_point
);
1577 return screen_point
;
1580 // Drag and drop ---------------------------------------------------------------
1582 int View::GetDragOperations(const gfx::Point
& press_pt
) {
1583 return drag_controller_
?
1584 drag_controller_
->GetDragOperationsForView(this, press_pt
) :
1585 ui::DragDropTypes::DRAG_NONE
;
1588 void View::WriteDragData(const gfx::Point
& press_pt
, OSExchangeData
* data
) {
1589 DCHECK(drag_controller_
);
1590 drag_controller_
->WriteDragDataForView(this, press_pt
, data
);
1593 bool View::InDrag() {
1594 Widget
* widget
= GetWidget();
1595 return widget
? widget
->dragged_view() == this : false;
1598 int View::GetHorizontalDragThreshold() {
1599 // TODO(jennyz): This value may need to be adjusted for different platforms
1600 // and for different display density.
1601 return kDefaultHorizontalDragThreshold
;
1604 int View::GetVerticalDragThreshold() {
1605 // TODO(jennyz): This value may need to be adjusted for different platforms
1606 // and for different display density.
1607 return kDefaultVerticalDragThreshold
;
1610 // Debugging -------------------------------------------------------------------
1612 #if !defined(NDEBUG)
1614 std::string
View::PrintViewGraph(bool first
) {
1615 return DoPrintViewGraph(first
, this);
1618 std::string
View::DoPrintViewGraph(bool first
, View
* view_with_children
) {
1619 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1620 const size_t kMaxPointerStringLength
= 19;
1625 result
.append("digraph {\n");
1627 // Node characteristics.
1628 char p
[kMaxPointerStringLength
];
1630 const std::string
class_name(GetClassName());
1631 size_t base_name_index
= class_name
.find_last_of('/');
1632 if (base_name_index
== std::string::npos
)
1633 base_name_index
= 0;
1637 char bounds_buffer
[512];
1639 // Information about current node.
1640 base::snprintf(p
, arraysize(bounds_buffer
), "%p", view_with_children
);
1641 result
.append(" N");
1642 result
.append(p
+ 2);
1643 result
.append(" [label=\"");
1645 result
.append(class_name
.substr(base_name_index
).c_str());
1647 base::snprintf(bounds_buffer
,
1648 arraysize(bounds_buffer
),
1649 "\\n bounds: (%d, %d), (%dx%d)",
1654 result
.append(bounds_buffer
);
1656 gfx::DecomposedTransform decomp
;
1657 if (!GetTransform().IsIdentity() &&
1658 gfx::DecomposeTransform(&decomp
, GetTransform())) {
1659 base::snprintf(bounds_buffer
,
1660 arraysize(bounds_buffer
),
1661 "\\n translation: (%f, %f)",
1662 decomp
.translate
[0],
1663 decomp
.translate
[1]);
1664 result
.append(bounds_buffer
);
1666 base::snprintf(bounds_buffer
,
1667 arraysize(bounds_buffer
),
1668 "\\n rotation: %3.2f",
1669 std::acos(decomp
.quaternion
[3]) * 360.0 / M_PI
);
1670 result
.append(bounds_buffer
);
1672 base::snprintf(bounds_buffer
,
1673 arraysize(bounds_buffer
),
1674 "\\n scale: (%2.4f, %2.4f)",
1677 result
.append(bounds_buffer
);
1680 result
.append("\"");
1682 result
.append(", shape=box");
1684 if (layer()->has_external_content())
1685 result
.append(", color=green");
1687 result
.append(", color=red");
1689 if (layer()->fills_bounds_opaquely())
1690 result
.append(", style=filled");
1692 result
.append("]\n");
1696 char pp
[kMaxPointerStringLength
];
1698 base::snprintf(pp
, kMaxPointerStringLength
, "%p", parent_
);
1699 result
.append(" N");
1700 result
.append(pp
+ 2);
1701 result
.append(" -> N");
1702 result
.append(p
+ 2);
1703 result
.append("\n");
1707 for (int i
= 0, count
= view_with_children
->child_count(); i
< count
; ++i
)
1708 result
.append(view_with_children
->child_at(i
)->PrintViewGraph(false));
1711 result
.append("}\n");
1717 ////////////////////////////////////////////////////////////////////////////////
1720 // DropInfo --------------------------------------------------------------------
1722 void View::DragInfo::Reset() {
1723 possible_drag
= false;
1724 start_pt
= gfx::Point();
1727 void View::DragInfo::PossibleDrag(const gfx::Point
& p
) {
1728 possible_drag
= true;
1732 // Painting --------------------------------------------------------------------
1734 void View::SchedulePaintBoundsChanged(SchedulePaintType type
) {
1735 // If we have a layer and the View's size did not change, we do not need to
1736 // schedule any paints since the layer will be redrawn at its new location
1737 // during the next Draw() cycle in the compositor.
1738 if (!layer() || type
== SCHEDULE_PAINT_SIZE_CHANGED
) {
1739 // Otherwise, if the size changes or we don't have a layer then we need to
1740 // use SchedulePaint to invalidate the area occupied by the View.
1742 } else if (parent_
&& type
== SCHEDULE_PAINT_SIZE_SAME
) {
1743 // The compositor doesn't Draw() until something on screen changes, so
1744 // if our position changes but nothing is being animated on screen, then
1745 // tell the compositor to redraw the scene. We know layer() exists due to
1746 // the above if clause.
1747 layer()->ScheduleDraw();
1751 void View::PaintCommon(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1756 // If the View we are about to paint requested the canvas to be flipped, we
1757 // should change the transform appropriately.
1758 // The canvas mirroring is undone once the View is done painting so that we
1759 // don't pass the canvas with the mirrored transform to Views that didn't
1760 // request the canvas to be flipped.
1761 gfx::ScopedCanvas
scoped(canvas
);
1762 if (FlipCanvasOnPaintForRTLUI()) {
1763 canvas
->Translate(gfx::Vector2d(width(), 0));
1764 canvas
->Scale(-1, 1);
1770 PaintChildren(canvas
, cull_set
);
1773 // Tree operations -------------------------------------------------------------
1775 void View::DoRemoveChildView(View
* view
,
1776 bool update_focus_cycle
,
1777 bool update_tool_tip
,
1778 bool delete_removed_view
,
1782 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
1783 scoped_ptr
<View
> view_to_be_deleted
;
1784 if (i
!= children_
.end()) {
1785 if (update_focus_cycle
) {
1786 // Let's remove the view from the focus traversal.
1787 View
* next_focusable
= view
->next_focusable_view_
;
1788 View
* prev_focusable
= view
->previous_focusable_view_
;
1790 prev_focusable
->next_focusable_view_
= next_focusable
;
1792 next_focusable
->previous_focusable_view_
= prev_focusable
;
1796 UnregisterChildrenForVisibleBoundsNotification(view
);
1797 if (view
->visible())
1798 view
->SchedulePaint();
1799 GetWidget()->NotifyWillRemoveView(view
);
1802 // Remove the bounds of this child and any of its descendants from our
1803 // paint root bounds tree.
1804 BoundsTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
1806 view
->RemoveRootBounds(bounds_tree
);
1808 view
->PropagateRemoveNotifications(this, new_parent
);
1809 view
->parent_
= NULL
;
1810 view
->UpdateLayerVisibility();
1812 if (delete_removed_view
&& !view
->owned_by_client_
)
1813 view_to_be_deleted
.reset(view
);
1818 if (update_tool_tip
)
1821 if (layout_manager_
.get())
1822 layout_manager_
->ViewRemoved(this, view
);
1825 void View::PropagateRemoveNotifications(View
* old_parent
, View
* new_parent
) {
1826 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1827 child_at(i
)->PropagateRemoveNotifications(old_parent
, new_parent
);
1829 ViewHierarchyChangedDetails
details(false, old_parent
, this, new_parent
);
1830 for (View
* v
= this; v
; v
= v
->parent_
)
1831 v
->ViewHierarchyChangedImpl(true, details
);
1834 void View::PropagateAddNotifications(
1835 const ViewHierarchyChangedDetails
& details
) {
1836 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1837 child_at(i
)->PropagateAddNotifications(details
);
1838 ViewHierarchyChangedImpl(true, details
);
1841 void View::PropagateNativeViewHierarchyChanged() {
1842 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1843 child_at(i
)->PropagateNativeViewHierarchyChanged();
1844 NativeViewHierarchyChanged();
1847 void View::ViewHierarchyChangedImpl(
1848 bool register_accelerators
,
1849 const ViewHierarchyChangedDetails
& details
) {
1850 if (register_accelerators
) {
1851 if (details
.is_add
) {
1852 // If you get this registration, you are part of a subtree that has been
1853 // added to the view hierarchy.
1854 if (GetFocusManager())
1855 RegisterPendingAccelerators();
1857 if (details
.child
== this)
1858 UnregisterAccelerators(true);
1862 if (details
.is_add
&& layer() && !layer()->parent()) {
1863 UpdateParentLayer();
1864 Widget
* widget
= GetWidget();
1866 widget
->UpdateRootLayers();
1867 } else if (!details
.is_add
&& details
.child
== this) {
1868 // Make sure the layers belonging to the subtree rooted at |child| get
1869 // removed from layers that do not belong in the same subtree.
1871 Widget
* widget
= GetWidget();
1873 widget
->UpdateRootLayers();
1876 ViewHierarchyChanged(details
);
1877 details
.parent
->needs_layout_
= true;
1880 void View::PropagateNativeThemeChanged(const ui::NativeTheme
* theme
) {
1881 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1882 child_at(i
)->PropagateNativeThemeChanged(theme
);
1883 OnNativeThemeChanged(theme
);
1886 // Size and disposition --------------------------------------------------------
1888 void View::PropagateVisibilityNotifications(View
* start
, bool is_visible
) {
1889 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1890 child_at(i
)->PropagateVisibilityNotifications(start
, is_visible
);
1891 VisibilityChangedImpl(start
, is_visible
);
1894 void View::VisibilityChangedImpl(View
* starting_from
, bool is_visible
) {
1895 VisibilityChanged(starting_from
, is_visible
);
1898 void View::BoundsChanged(const gfx::Rect
& previous_bounds
) {
1899 // Mark our bounds as dirty for the paint root, also see if we need to
1900 // recompute our children's bounds due to origin change.
1901 bool origin_changed
=
1902 previous_bounds
.OffsetFromOrigin() != bounds_
.OffsetFromOrigin();
1903 SetRootBoundsDirty(origin_changed
);
1906 // Paint the new bounds.
1907 SchedulePaintBoundsChanged(
1908 bounds_
.size() == previous_bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
1909 SCHEDULE_PAINT_SIZE_CHANGED
);
1914 SetLayerBounds(GetLocalBounds() +
1915 gfx::Vector2d(GetMirroredX(), y()) +
1916 parent_
->CalculateOffsetToAncestorWithLayer(NULL
));
1918 SetLayerBounds(bounds_
);
1921 // If our bounds have changed, then any descendant layer bounds may have
1922 // changed. Update them accordingly.
1923 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
1926 OnBoundsChanged(previous_bounds
);
1928 if (previous_bounds
.size() != size()) {
1929 needs_layout_
= false;
1933 if (GetNeedsNotificationWhenVisibleBoundsChange())
1934 OnVisibleBoundsChanged();
1936 // Notify interested Views that visible bounds within the root view may have
1938 if (descendants_to_notify_
.get()) {
1939 for (Views::iterator
i(descendants_to_notify_
->begin());
1940 i
!= descendants_to_notify_
->end(); ++i
) {
1941 (*i
)->OnVisibleBoundsChanged();
1947 void View::RegisterChildrenForVisibleBoundsNotification(View
* view
) {
1948 if (view
->GetNeedsNotificationWhenVisibleBoundsChange())
1949 view
->RegisterForVisibleBoundsNotification();
1950 for (int i
= 0; i
< view
->child_count(); ++i
)
1951 RegisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
1955 void View::UnregisterChildrenForVisibleBoundsNotification(View
* view
) {
1956 if (view
->GetNeedsNotificationWhenVisibleBoundsChange())
1957 view
->UnregisterForVisibleBoundsNotification();
1958 for (int i
= 0; i
< view
->child_count(); ++i
)
1959 UnregisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
1962 void View::RegisterForVisibleBoundsNotification() {
1963 if (registered_for_visible_bounds_notification_
)
1966 registered_for_visible_bounds_notification_
= true;
1967 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
1968 ancestor
->AddDescendantToNotify(this);
1971 void View::UnregisterForVisibleBoundsNotification() {
1972 if (!registered_for_visible_bounds_notification_
)
1975 registered_for_visible_bounds_notification_
= false;
1976 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
1977 ancestor
->RemoveDescendantToNotify(this);
1980 void View::AddDescendantToNotify(View
* view
) {
1982 if (!descendants_to_notify_
.get())
1983 descendants_to_notify_
.reset(new Views
);
1984 descendants_to_notify_
->push_back(view
);
1987 void View::RemoveDescendantToNotify(View
* view
) {
1988 DCHECK(view
&& descendants_to_notify_
.get());
1989 Views::iterator
i(std::find(
1990 descendants_to_notify_
->begin(), descendants_to_notify_
->end(), view
));
1991 DCHECK(i
!= descendants_to_notify_
->end());
1992 descendants_to_notify_
->erase(i
);
1993 if (descendants_to_notify_
->empty())
1994 descendants_to_notify_
.reset();
1997 void View::SetLayerBounds(const gfx::Rect
& bounds
) {
1998 layer()->SetBounds(bounds
);
1999 SnapLayerToPixelBoundary();
2002 void View::SetRootBoundsDirty(bool origin_changed
) {
2003 root_bounds_dirty_
= true;
2005 if (origin_changed
) {
2006 // Inform our children that their root bounds are dirty, as their relative
2007 // coordinates in paint root space have changed since ours have changed.
2008 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end();
2010 if (!(*i
)->IsPaintRoot())
2011 (*i
)->SetRootBoundsDirty(origin_changed
);
2016 void View::UpdateRootBounds(BoundsTree
* tree
, const gfx::Vector2d
& offset
) {
2017 // If we're not visible no need to update BoundsTree. When we are made visible
2018 // the BoundsTree will be updated appropriately.
2022 if (!root_bounds_dirty_
&& children_
.empty())
2025 // No need to recompute bounds if we haven't flagged ours as dirty.
2026 TRACE_EVENT1("views", "View::UpdateRootBounds", "class", GetClassName());
2028 // Add our own offset to the provided offset, for our own bounds update and
2029 // for propagation to our children if needed.
2030 gfx::Vector2d view_offset
= offset
+ GetMirroredBounds().OffsetFromOrigin();
2032 // If our bounds have changed we must re-insert our new bounds to the tree.
2033 if (root_bounds_dirty_
) {
2034 root_bounds_dirty_
= false;
2036 view_offset
.x(), view_offset
.y(), bounds_
.width(), bounds_
.height());
2037 tree
->Insert(bounds
, reinterpret_cast<intptr_t>(this));
2040 // Update our children's bounds if needed.
2041 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2042 // We don't descend in to layer views for bounds recomputation, as they
2043 // manage their own RTree as paint roots.
2044 if (!(*i
)->IsPaintRoot())
2045 (*i
)->UpdateRootBounds(tree
, view_offset
);
2049 void View::RemoveRootBounds(BoundsTree
* tree
) {
2050 tree
->Remove(reinterpret_cast<intptr_t>(this));
2052 root_bounds_dirty_
= true;
2054 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2055 if (!(*i
)->IsPaintRoot())
2056 (*i
)->RemoveRootBounds(tree
);
2060 View::BoundsTree
* View::GetBoundsTreeFromPaintRoot() {
2061 BoundsTree
* bounds_tree
= bounds_tree_
.get();
2062 View
* paint_root
= this;
2063 while (!bounds_tree
&& !paint_root
->IsPaintRoot()) {
2064 // Assumption is that if IsPaintRoot() is false then parent_ is valid.
2066 paint_root
= paint_root
->parent_
;
2067 bounds_tree
= paint_root
->bounds_tree_
.get();
2073 // Transformations -------------------------------------------------------------
2075 bool View::GetTransformRelativeTo(const View
* ancestor
,
2076 gfx::Transform
* transform
) const {
2077 const View
* p
= this;
2079 while (p
&& p
!= ancestor
) {
2080 transform
->ConcatTransform(p
->GetTransform());
2081 gfx::Transform translation
;
2082 translation
.Translate(static_cast<float>(p
->GetMirroredX()),
2083 static_cast<float>(p
->y()));
2084 transform
->ConcatTransform(translation
);
2089 return p
== ancestor
;
2092 // Coordinate conversion -------------------------------------------------------
2094 bool View::ConvertPointForAncestor(const View
* ancestor
,
2095 gfx::Point
* point
) const {
2096 gfx::Transform trans
;
2097 // TODO(sad): Have some way of caching the transformation results.
2098 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2099 gfx::Point3F
p(*point
);
2100 trans
.TransformPoint(&p
);
2101 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2105 bool View::ConvertPointFromAncestor(const View
* ancestor
,
2106 gfx::Point
* point
) const {
2107 gfx::Transform trans
;
2108 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2109 gfx::Point3F
p(*point
);
2110 trans
.TransformPointReverse(&p
);
2111 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2115 bool View::ConvertRectForAncestor(const View
* ancestor
,
2116 gfx::RectF
* rect
) const {
2117 gfx::Transform trans
;
2118 // TODO(sad): Have some way of caching the transformation results.
2119 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2120 trans
.TransformRect(rect
);
2124 bool View::ConvertRectFromAncestor(const View
* ancestor
,
2125 gfx::RectF
* rect
) const {
2126 gfx::Transform trans
;
2127 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2128 trans
.TransformRectReverse(rect
);
2132 // Accelerated painting --------------------------------------------------------
2134 void View::CreateLayer() {
2135 // A new layer is being created for the view. So all the layers of the
2136 // sub-tree can inherit the visibility of the corresponding view.
2137 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2138 child_at(i
)->UpdateChildLayerVisibility(true);
2140 SetLayer(new ui::Layer());
2141 layer()->set_delegate(this);
2142 #if !defined(NDEBUG)
2143 layer()->set_name(GetClassName());
2146 UpdateParentLayers();
2147 UpdateLayerVisibility();
2149 // The new layer needs to be ordered in the layer tree according
2150 // to the view tree. Children of this layer were added in order
2151 // in UpdateParentLayers().
2153 parent()->ReorderLayers();
2155 Widget
* widget
= GetWidget();
2157 widget
->UpdateRootLayers();
2160 void View::UpdateParentLayers() {
2161 // Attach all top-level un-parented layers.
2162 if (layer() && !layer()->parent()) {
2163 UpdateParentLayer();
2165 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2166 child_at(i
)->UpdateParentLayers();
2170 void View::OrphanLayers() {
2172 if (layer()->parent())
2173 layer()->parent()->Remove(layer());
2175 // The layer belonging to this View has already been orphaned. It is not
2176 // necessary to orphan the child layers.
2179 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2180 child_at(i
)->OrphanLayers();
2183 void View::ReparentLayer(const gfx::Vector2d
& offset
, ui::Layer
* parent_layer
) {
2184 layer()->SetBounds(GetLocalBounds() + offset
);
2185 DCHECK_NE(layer(), parent_layer
);
2187 parent_layer
->Add(layer());
2188 layer()->SchedulePaint(GetLocalBounds());
2189 MoveLayerToParent(layer(), gfx::Point());
2192 void View::DestroyLayer() {
2193 ui::Layer
* new_parent
= layer()->parent();
2194 std::vector
<ui::Layer
*> children
= layer()->children();
2195 for (size_t i
= 0; i
< children
.size(); ++i
) {
2196 layer()->Remove(children
[i
]);
2198 new_parent
->Add(children
[i
]);
2201 LayerOwner::DestroyLayer();
2206 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
2210 Widget
* widget
= GetWidget();
2212 widget
->UpdateRootLayers();
2215 // Input -----------------------------------------------------------------------
2217 bool View::ProcessMousePressed(const ui::MouseEvent
& event
) {
2218 int drag_operations
=
2219 (enabled_
&& event
.IsOnlyLeftMouseButton() &&
2220 HitTestPoint(event
.location())) ?
2221 GetDragOperations(event
.location()) : 0;
2222 ContextMenuController
* context_menu_controller
= event
.IsRightMouseButton() ?
2223 context_menu_controller_
: 0;
2224 View::DragInfo
* drag_info
= GetDragInfo();
2226 // TODO(sky): for debugging 360238.
2228 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2229 kContextMenuOnMousePress
&& HitTestPoint(event
.location())) {
2230 ViewStorage
* view_storage
= ViewStorage::GetInstance();
2231 storage_id
= view_storage
->CreateStorageID();
2232 view_storage
->StoreView(storage_id
, this);
2235 const bool enabled
= enabled_
;
2236 const bool result
= OnMousePressed(event
);
2241 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2242 kContextMenuOnMousePress
) {
2243 // Assume that if there is a context menu controller we won't be deleted
2244 // from mouse pressed.
2245 gfx::Point
location(event
.location());
2246 if (HitTestPoint(location
)) {
2247 if (storage_id
!= 0)
2248 CHECK_EQ(this, ViewStorage::GetInstance()->RetrieveView(storage_id
));
2249 ConvertPointToScreen(this, &location
);
2250 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2255 // WARNING: we may have been deleted, don't use any View variables.
2256 if (drag_operations
!= ui::DragDropTypes::DRAG_NONE
) {
2257 drag_info
->PossibleDrag(event
.location());
2260 return !!context_menu_controller
|| result
;
2263 bool View::ProcessMouseDragged(const ui::MouseEvent
& event
) {
2264 CHECK_EQ(ui::ET_MOUSE_DRAGGED
, event
.type());
2266 // Copy the field, that way if we're deleted after drag and drop no harm is
2268 ContextMenuController
* context_menu_controller
= context_menu_controller_
;
2269 const bool possible_drag
= GetDragInfo()->possible_drag
;
2270 if (possible_drag
&&
2271 ExceededDragThreshold(GetDragInfo()->start_pt
- event
.location()) &&
2272 (!drag_controller_
||
2273 drag_controller_
->CanStartDragForView(
2274 this, GetDragInfo()->start_pt
, event
.location()))) {
2275 DoDrag(event
, GetDragInfo()->start_pt
,
2276 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
);
2278 if (OnMouseDragged(event
))
2280 // Fall through to return value based on context menu controller.
2282 // WARNING: we may have been deleted.
2283 return (context_menu_controller
!= NULL
) || possible_drag
;
2286 void View::ProcessMouseReleased(const ui::MouseEvent
& event
) {
2287 if (!kContextMenuOnMousePress
&& context_menu_controller_
&&
2288 event
.IsOnlyRightMouseButton()) {
2289 // Assume that if there is a context menu controller we won't be deleted
2290 // from mouse released.
2291 gfx::Point
location(event
.location());
2292 OnMouseReleased(event
);
2293 if (HitTestPoint(location
)) {
2294 ConvertPointToScreen(this, &location
);
2295 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2298 OnMouseReleased(event
);
2300 // WARNING: we may have been deleted.
2303 // Accelerators ----------------------------------------------------------------
2305 void View::RegisterPendingAccelerators() {
2306 if (!accelerators_
.get() ||
2307 registered_accelerator_count_
== accelerators_
->size()) {
2308 // No accelerators are waiting for registration.
2313 // The view is not yet attached to a widget, defer registration until then.
2317 accelerator_focus_manager_
= GetFocusManager();
2318 if (!accelerator_focus_manager_
) {
2319 // Some crash reports seem to show that we may get cases where we have no
2320 // focus manager (see bug #1291225). This should never be the case, just
2321 // making sure we don't crash.
2325 for (std::vector
<ui::Accelerator
>::const_iterator
i(
2326 accelerators_
->begin() + registered_accelerator_count_
);
2327 i
!= accelerators_
->end(); ++i
) {
2328 accelerator_focus_manager_
->RegisterAccelerator(
2329 *i
, ui::AcceleratorManager::kNormalPriority
, this);
2331 registered_accelerator_count_
= accelerators_
->size();
2334 void View::UnregisterAccelerators(bool leave_data_intact
) {
2335 if (!accelerators_
.get())
2339 if (accelerator_focus_manager_
) {
2340 accelerator_focus_manager_
->UnregisterAccelerators(this);
2341 accelerator_focus_manager_
= NULL
;
2343 if (!leave_data_intact
) {
2344 accelerators_
->clear();
2345 accelerators_
.reset();
2347 registered_accelerator_count_
= 0;
2351 // Focus -----------------------------------------------------------------------
2353 void View::InitFocusSiblings(View
* v
, int index
) {
2354 int count
= child_count();
2357 v
->next_focusable_view_
= NULL
;
2358 v
->previous_focusable_view_
= NULL
;
2360 if (index
== count
) {
2361 // We are inserting at the end, but the end of the child list may not be
2362 // the last focusable element. Let's try to find an element with no next
2363 // focusable element to link to.
2364 View
* last_focusable_view
= NULL
;
2365 for (Views::iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2366 if (!(*i
)->next_focusable_view_
) {
2367 last_focusable_view
= *i
;
2371 if (last_focusable_view
== NULL
) {
2372 // Hum... there is a cycle in the focus list. Let's just insert ourself
2373 // after the last child.
2374 View
* prev
= children_
[index
- 1];
2375 v
->previous_focusable_view_
= prev
;
2376 v
->next_focusable_view_
= prev
->next_focusable_view_
;
2377 prev
->next_focusable_view_
->previous_focusable_view_
= v
;
2378 prev
->next_focusable_view_
= v
;
2380 last_focusable_view
->next_focusable_view_
= v
;
2381 v
->next_focusable_view_
= NULL
;
2382 v
->previous_focusable_view_
= last_focusable_view
;
2385 View
* prev
= children_
[index
]->GetPreviousFocusableView();
2386 v
->previous_focusable_view_
= prev
;
2387 v
->next_focusable_view_
= children_
[index
];
2389 prev
->next_focusable_view_
= v
;
2390 children_
[index
]->previous_focusable_view_
= v
;
2395 void View::AdvanceFocusIfNecessary() {
2396 // Focus should only be advanced if this is the focused view and has become
2397 // unfocusable. If the view is still focusable or is not focused, we can
2398 // return early avoiding furthur unnecessary checks. Focusability check is
2399 // performed first as it tends to be faster.
2400 if (IsAccessibilityFocusable() || !HasFocus())
2403 FocusManager
* focus_manager
= GetFocusManager();
2405 focus_manager
->AdvanceFocusIfNecessary();
2408 // System events ---------------------------------------------------------------
2410 void View::PropagateThemeChanged() {
2411 for (int i
= child_count() - 1; i
>= 0; --i
)
2412 child_at(i
)->PropagateThemeChanged();
2416 void View::PropagateLocaleChanged() {
2417 for (int i
= child_count() - 1; i
>= 0; --i
)
2418 child_at(i
)->PropagateLocaleChanged();
2422 // Tooltips --------------------------------------------------------------------
2424 void View::UpdateTooltip() {
2425 Widget
* widget
= GetWidget();
2426 // TODO(beng): The TooltipManager NULL check can be removed when we
2427 // consolidate Init() methods and make views_unittests Init() all
2428 // Widgets that it uses.
2429 if (widget
&& widget
->GetTooltipManager())
2430 widget
->GetTooltipManager()->UpdateTooltip();
2433 // Drag and drop ---------------------------------------------------------------
2435 bool View::DoDrag(const ui::LocatedEvent
& event
,
2436 const gfx::Point
& press_pt
,
2437 ui::DragDropTypes::DragEventSource source
) {
2438 int drag_operations
= GetDragOperations(press_pt
);
2439 if (drag_operations
== ui::DragDropTypes::DRAG_NONE
)
2442 Widget
* widget
= GetWidget();
2443 // We should only start a drag from an event, implying we have a widget.
2446 // Don't attempt to start a drag while in the process of dragging. This is
2447 // especially important on X where we can get multiple mouse move events when
2448 // we start the drag.
2449 if (widget
->dragged_view())
2452 OSExchangeData data
;
2453 WriteDragData(press_pt
, &data
);
2455 // Message the RootView to do the drag and drop. That way if we're removed
2456 // the RootView can detect it and avoid calling us back.
2457 gfx::Point
widget_location(event
.location());
2458 ConvertPointToWidget(this, &widget_location
);
2459 widget
->RunShellDrag(this, data
, widget_location
, drag_operations
, source
);
2460 // WARNING: we may have been deleted.
2464 } // namespace views