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/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/trace_event/trace_event.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/geometry/point3_f.h"
29 #include "ui/gfx/geometry/point_conversions.h"
30 #include "ui/gfx/interpolated_transform.h"
31 #include "ui/gfx/path.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"
59 const bool kContextMenuOnMousePress
= false;
61 const bool kContextMenuOnMousePress
= true;
64 // Default horizontal drag threshold in pixels.
65 // Same as what gtk uses.
66 const int kDefaultHorizontalDragThreshold
= 8;
68 // Default vertical drag threshold in pixels.
69 // Same as what gtk uses.
70 const int kDefaultVerticalDragThreshold
= 8;
72 // Returns the top view in |view|'s hierarchy.
73 const View
* GetHierarchyRoot(const View
* view
) {
74 const View
* root
= view
;
75 while (root
&& root
->parent())
76 root
= root
->parent();
83 ViewsDelegate
* ViewsDelegate::views_delegate
= NULL
;
86 const char View::kViewClassName
[] = "View";
88 ////////////////////////////////////////////////////////////////////////////////
91 // Creation and lifetime -------------------------------------------------------
94 : owned_by_client_(false),
100 notify_enter_exit_on_child_(false),
101 registered_for_visible_bounds_notification_(false),
102 root_bounds_dirty_(true),
103 clip_insets_(0, 0, 0, 0),
105 snap_layer_to_pixel_boundary_(false),
106 flip_canvas_on_paint_for_rtl_ui_(false),
107 paint_to_layer_(false),
108 accelerator_focus_manager_(NULL
),
109 registered_accelerator_count_(0),
110 next_focusable_view_(NULL
),
111 previous_focusable_view_(NULL
),
113 accessibility_focusable_(false),
114 context_menu_controller_(NULL
),
115 drag_controller_(NULL
),
116 native_view_accessibility_(NULL
) {
121 parent_
->RemoveChildView(this);
123 ViewStorage::GetInstance()->ViewRemoved(this);
125 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
126 (*i
)->parent_
= NULL
;
127 if (!(*i
)->owned_by_client_
)
131 // Release ownership of the native accessibility object, but it's
132 // reference-counted on some platforms, so it may not be deleted right away.
133 if (native_view_accessibility_
)
134 native_view_accessibility_
->Destroy();
137 // Tree operations -------------------------------------------------------------
139 const Widget
* View::GetWidget() const {
140 // The root view holds a reference to this view hierarchy's Widget.
141 return parent_
? parent_
->GetWidget() : NULL
;
144 Widget
* View::GetWidget() {
145 return const_cast<Widget
*>(const_cast<const View
*>(this)->GetWidget());
148 void View::AddChildView(View
* view
) {
149 if (view
->parent_
== this)
151 AddChildViewAt(view
, child_count());
154 void View::AddChildViewAt(View
* view
, int index
) {
155 CHECK_NE(view
, this) << "You cannot add a view as its own child";
157 DCHECK_LE(index
, child_count());
159 // If |view| has a parent, remove it from its parent.
160 View
* parent
= view
->parent_
;
161 ui::NativeTheme
* old_theme
= NULL
;
163 old_theme
= view
->GetNativeTheme();
164 if (parent
== this) {
165 ReorderChildView(view
, index
);
168 parent
->DoRemoveChildView(view
, true, true, false, this);
171 // Sets the prev/next focus views.
172 InitFocusSiblings(view
, index
);
174 // Let's insert the view.
175 view
->parent_
= this;
176 children_
.insert(children_
.begin() + index
, view
);
178 // Instruct the view to recompute its root bounds on next Paint().
179 view
->SetRootBoundsDirty(true);
181 views::Widget
* widget
= GetWidget();
183 const ui::NativeTheme
* new_theme
= view
->GetNativeTheme();
184 if (new_theme
!= old_theme
)
185 view
->PropagateNativeThemeChanged(new_theme
);
188 ViewHierarchyChangedDetails
details(true, this, view
, parent
);
190 for (View
* v
= this; v
; v
= v
->parent_
)
191 v
->ViewHierarchyChangedImpl(false, details
);
193 view
->PropagateAddNotifications(details
);
196 RegisterChildrenForVisibleBoundsNotification(view
);
198 view
->SchedulePaint();
201 if (layout_manager_
.get())
202 layout_manager_
->ViewAdded(this, view
);
206 // Make sure the visibility of the child layers are correct.
207 // If any of the parent View is hidden, then the layers of the subtree
208 // rooted at |this| should be hidden. Otherwise, all the child layers should
209 // inherit the visibility of the owner View.
210 UpdateLayerVisibility();
213 void View::ReorderChildView(View
* view
, int index
) {
214 DCHECK_EQ(view
->parent_
, this);
216 index
= child_count() - 1;
217 else if (index
>= child_count())
219 if (children_
[index
] == view
)
222 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
223 DCHECK(i
!= children_
.end());
226 // Unlink the view first
227 View
* next_focusable
= view
->next_focusable_view_
;
228 View
* prev_focusable
= view
->previous_focusable_view_
;
230 prev_focusable
->next_focusable_view_
= next_focusable
;
232 next_focusable
->previous_focusable_view_
= prev_focusable
;
234 // Add it in the specified index now.
235 InitFocusSiblings(view
, index
);
236 children_
.insert(children_
.begin() + index
, view
);
241 void View::RemoveChildView(View
* view
) {
242 DoRemoveChildView(view
, true, true, false, NULL
);
245 void View::RemoveAllChildViews(bool delete_children
) {
246 while (!children_
.empty())
247 DoRemoveChildView(children_
.front(), false, false, delete_children
, NULL
);
251 bool View::Contains(const View
* view
) const {
252 for (const View
* v
= view
; v
; v
= v
->parent_
) {
259 int View::GetIndexOf(const View
* view
) const {
260 Views::const_iterator
i(std::find(children_
.begin(), children_
.end(), view
));
261 return i
!= children_
.end() ? static_cast<int>(i
- children_
.begin()) : -1;
264 // Size and disposition --------------------------------------------------------
266 void View::SetBounds(int x
, int y
, int width
, int height
) {
267 SetBoundsRect(gfx::Rect(x
, y
, std::max(0, width
), std::max(0, height
)));
270 void View::SetBoundsRect(const gfx::Rect
& bounds
) {
271 if (bounds
== bounds_
) {
273 needs_layout_
= false;
280 // Paint where the view is currently.
281 SchedulePaintBoundsChanged(
282 bounds_
.size() == bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
283 SCHEDULE_PAINT_SIZE_CHANGED
);
286 gfx::Rect prev
= bounds_
;
291 void View::SetSize(const gfx::Size
& size
) {
292 SetBounds(x(), y(), size
.width(), size
.height());
295 void View::SetPosition(const gfx::Point
& position
) {
296 SetBounds(position
.x(), position
.y(), width(), height());
299 void View::SetX(int x
) {
300 SetBounds(x
, y(), width(), height());
303 void View::SetY(int y
) {
304 SetBounds(x(), y
, width(), height());
307 gfx::Rect
View::GetContentsBounds() const {
308 gfx::Rect
contents_bounds(GetLocalBounds());
310 contents_bounds
.Inset(border_
->GetInsets());
311 return contents_bounds
;
314 gfx::Rect
View::GetLocalBounds() const {
315 return gfx::Rect(size());
318 gfx::Rect
View::GetLayerBoundsInPixel() const {
319 return layer()->GetTargetBounds();
322 gfx::Insets
View::GetInsets() const {
323 return border_
.get() ? border_
->GetInsets() : gfx::Insets();
326 gfx::Rect
View::GetVisibleBounds() const {
329 gfx::Rect
vis_bounds(GetLocalBounds());
330 gfx::Rect ancestor_bounds
;
331 const View
* view
= this;
332 gfx::Transform transform
;
334 while (view
!= NULL
&& !vis_bounds
.IsEmpty()) {
335 transform
.ConcatTransform(view
->GetTransform());
336 gfx::Transform translation
;
337 translation
.Translate(static_cast<float>(view
->GetMirroredX()),
338 static_cast<float>(view
->y()));
339 transform
.ConcatTransform(translation
);
341 vis_bounds
= view
->ConvertRectToParent(vis_bounds
);
342 const View
* ancestor
= view
->parent_
;
343 if (ancestor
!= NULL
) {
344 ancestor_bounds
.SetRect(0, 0, ancestor
->width(), ancestor
->height());
345 vis_bounds
.Intersect(ancestor_bounds
);
346 } else if (!view
->GetWidget()) {
347 // If the view has no Widget, we're not visible. Return an empty rect.
352 if (vis_bounds
.IsEmpty())
354 // Convert back to this views coordinate system.
355 gfx::RectF
views_vis_bounds(vis_bounds
);
356 transform
.TransformRectReverse(&views_vis_bounds
);
357 // Partially visible pixels should be considered visible.
358 return gfx::ToEnclosingRect(views_vis_bounds
);
361 gfx::Rect
View::GetBoundsInScreen() const {
363 View::ConvertPointToScreen(this, &origin
);
364 return gfx::Rect(origin
, size());
367 gfx::Size
View::GetPreferredSize() const {
368 if (layout_manager_
.get())
369 return layout_manager_
->GetPreferredSize(this);
373 int View::GetBaseline() const {
377 void View::SizeToPreferredSize() {
378 gfx::Size prefsize
= GetPreferredSize();
379 if ((prefsize
.width() != width()) || (prefsize
.height() != height()))
380 SetBounds(x(), y(), prefsize
.width(), prefsize
.height());
383 gfx::Size
View::GetMinimumSize() const {
384 return GetPreferredSize();
387 gfx::Size
View::GetMaximumSize() const {
391 int View::GetHeightForWidth(int w
) const {
392 if (layout_manager_
.get())
393 return layout_manager_
->GetPreferredHeightForWidth(this, w
);
394 return GetPreferredSize().height();
397 void View::SetVisible(bool visible
) {
398 if (visible
!= visible_
) {
399 // If the View is currently visible, schedule paint to refresh parent.
400 // TODO(beng): not sure we should be doing this if we have a layer.
405 AdvanceFocusIfNecessary();
407 // Notify the parent.
409 parent_
->ChildVisibilityChanged(this);
411 // This notifies all sub-views recursively.
412 PropagateVisibilityNotifications(this, visible_
);
413 UpdateLayerVisibility();
415 // If we are newly visible, schedule paint.
419 // We're never painted when hidden, so no need to be in the BoundsTree.
420 BoundsTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
422 RemoveRootBounds(bounds_tree
);
427 bool View::IsDrawn() const {
428 return visible_
&& parent_
? parent_
->IsDrawn() : false;
431 void View::SetEnabled(bool enabled
) {
432 if (enabled
!= enabled_
) {
434 AdvanceFocusIfNecessary();
439 void View::OnEnabledChanged() {
443 // Transformations -------------------------------------------------------------
445 gfx::Transform
View::GetTransform() const {
446 return layer() ? layer()->transform() : gfx::Transform();
449 void View::SetTransform(const gfx::Transform
& transform
) {
450 if (transform
.IsIdentity()) {
452 layer()->SetTransform(transform
);
453 if (!paint_to_layer_
)
461 layer()->SetTransform(transform
);
462 layer()->ScheduleDraw();
466 void View::SetPaintToLayer(bool paint_to_layer
) {
467 if (paint_to_layer_
== paint_to_layer
)
470 // If this is a change in state we will also need to update bounds trees.
471 if (paint_to_layer
) {
472 // Gaining a layer means becoming a paint root. We must remove ourselves
473 // from our old paint root, if we had one. Traverse up view tree to find old
475 View
* old_paint_root
= parent_
;
476 while (old_paint_root
&& !old_paint_root
->IsPaintRoot())
477 old_paint_root
= old_paint_root
->parent_
;
479 // Remove our and our children's bounds from the old tree. This will also
480 // mark all of our bounds as dirty.
481 if (old_paint_root
&& old_paint_root
->bounds_tree_
)
482 RemoveRootBounds(old_paint_root
->bounds_tree_
.get());
485 // Losing a layer means we are no longer a paint root, so delete our
486 // bounds tree and mark ourselves as dirty for future insertion into our
487 // new paint root's bounds tree.
488 bounds_tree_
.reset();
489 SetRootBoundsDirty(true);
492 paint_to_layer_
= paint_to_layer
;
493 if (paint_to_layer_
&& !layer()) {
495 } else if (!paint_to_layer_
&& layer()) {
500 // RTL positioning -------------------------------------------------------------
502 gfx::Rect
View::GetMirroredBounds() const {
503 gfx::Rect
bounds(bounds_
);
504 bounds
.set_x(GetMirroredX());
508 gfx::Point
View::GetMirroredPosition() const {
509 return gfx::Point(GetMirroredX(), y());
512 int View::GetMirroredX() const {
513 return parent_
? parent_
->GetMirroredXForRect(bounds_
) : x();
516 int View::GetMirroredXForRect(const gfx::Rect
& bounds
) const {
517 return base::i18n::IsRTL() ?
518 (width() - bounds
.x() - bounds
.width()) : bounds
.x();
521 int View::GetMirroredXInView(int x
) const {
522 return base::i18n::IsRTL() ? width() - x
: x
;
525 int View::GetMirroredXWithWidthInView(int x
, int w
) const {
526 return base::i18n::IsRTL() ? width() - x
- w
: x
;
529 // Layout ----------------------------------------------------------------------
531 void View::Layout() {
532 needs_layout_
= false;
534 // If we have a layout manager, let it handle the layout for us.
535 if (layout_manager_
.get())
536 layout_manager_
->Layout(this);
538 // Make sure to propagate the Layout() call to any children that haven't
539 // received it yet through the layout manager and need to be laid out. This
540 // is needed for the case when the child requires a layout but its bounds
541 // weren't changed by the layout manager. If there is no layout manager, we
542 // just propagate the Layout() call down the hierarchy, so whoever receives
543 // the call can take appropriate action.
544 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
545 View
* child
= child_at(i
);
546 if (child
->needs_layout_
|| !layout_manager_
.get()) {
547 TRACE_EVENT1("views", "View::Layout", "class", child
->GetClassName());
548 child
->needs_layout_
= false;
554 void View::InvalidateLayout() {
555 // Always invalidate up. This is needed to handle the case of us already being
556 // valid, but not our parent.
557 needs_layout_
= true;
559 parent_
->InvalidateLayout();
562 LayoutManager
* View::GetLayoutManager() const {
563 return layout_manager_
.get();
566 void View::SetLayoutManager(LayoutManager
* layout_manager
) {
567 if (layout_manager_
.get())
568 layout_manager_
->Uninstalled(this);
570 layout_manager_
.reset(layout_manager
);
571 if (layout_manager_
.get())
572 layout_manager_
->Installed(this);
575 void View::SnapLayerToPixelBoundary() {
579 if (snap_layer_to_pixel_boundary_
&& layer()->parent() &&
580 layer()->GetCompositor()) {
581 ui::SnapLayerToPhysicalPixelBoundary(layer()->parent(), layer());
584 layer()->SetSubpixelPositionOffset(gfx::Vector2dF());
588 // Attributes ------------------------------------------------------------------
590 const char* View::GetClassName() const {
591 return kViewClassName
;
594 const View
* View::GetAncestorWithClassName(const std::string
& name
) const {
595 for (const View
* view
= this; view
; view
= view
->parent_
) {
596 if (!strcmp(view
->GetClassName(), name
.c_str()))
602 View
* View::GetAncestorWithClassName(const std::string
& name
) {
603 return const_cast<View
*>(const_cast<const View
*>(this)->
604 GetAncestorWithClassName(name
));
607 const View
* View::GetViewByID(int id
) const {
609 return const_cast<View
*>(this);
611 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
612 const View
* view
= child_at(i
)->GetViewByID(id
);
619 View
* View::GetViewByID(int id
) {
620 return const_cast<View
*>(const_cast<const View
*>(this)->GetViewByID(id
));
623 void View::SetGroup(int gid
) {
624 // Don't change the group id once it's set.
625 DCHECK(group_
== -1 || group_
== gid
);
629 int View::GetGroup() const {
633 bool View::IsGroupFocusTraversable() const {
637 void View::GetViewsInGroup(int group
, Views
* views
) {
639 views
->push_back(this);
641 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
642 child_at(i
)->GetViewsInGroup(group
, views
);
645 View
* View::GetSelectedViewForGroup(int group
) {
647 GetWidget()->GetRootView()->GetViewsInGroup(group
, &views
);
648 return views
.empty() ? NULL
: views
[0];
651 // Coordinate conversion -------------------------------------------------------
654 void View::ConvertPointToTarget(const View
* source
,
659 if (source
== target
)
662 const View
* root
= GetHierarchyRoot(target
);
663 CHECK_EQ(GetHierarchyRoot(source
), root
);
666 source
->ConvertPointForAncestor(root
, point
);
669 target
->ConvertPointFromAncestor(root
, point
);
673 void View::ConvertRectToTarget(const View
* source
,
678 if (source
== target
)
681 const View
* root
= GetHierarchyRoot(target
);
682 CHECK_EQ(GetHierarchyRoot(source
), root
);
685 source
->ConvertRectForAncestor(root
, rect
);
688 target
->ConvertRectFromAncestor(root
, rect
);
692 void View::ConvertPointToWidget(const View
* src
, gfx::Point
* p
) {
696 src
->ConvertPointForAncestor(NULL
, p
);
700 void View::ConvertPointFromWidget(const View
* dest
, gfx::Point
* p
) {
704 dest
->ConvertPointFromAncestor(NULL
, p
);
708 void View::ConvertPointToScreen(const View
* src
, gfx::Point
* p
) {
712 // If the view is not connected to a tree, there's nothing we can do.
713 const Widget
* widget
= src
->GetWidget();
715 ConvertPointToWidget(src
, p
);
716 *p
+= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
721 void View::ConvertPointFromScreen(const View
* dst
, gfx::Point
* p
) {
725 const views::Widget
* widget
= dst
->GetWidget();
728 *p
-= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
729 views::View::ConvertPointFromWidget(dst
, p
);
732 gfx::Rect
View::ConvertRectToParent(const gfx::Rect
& rect
) const {
733 gfx::RectF x_rect
= rect
;
734 GetTransform().TransformRect(&x_rect
);
735 x_rect
.Offset(GetMirroredPosition().OffsetFromOrigin());
736 // Pixels we partially occupy in the parent should be included.
737 return gfx::ToEnclosingRect(x_rect
);
740 gfx::Rect
View::ConvertRectToWidget(const gfx::Rect
& rect
) const {
741 gfx::Rect x_rect
= rect
;
742 for (const View
* v
= this; v
; v
= v
->parent_
)
743 x_rect
= v
->ConvertRectToParent(x_rect
);
747 // Painting --------------------------------------------------------------------
749 void View::SchedulePaint() {
750 SchedulePaintInRect(GetLocalBounds());
753 void View::SchedulePaintInRect(const gfx::Rect
& rect
) {
758 layer()->SchedulePaint(rect
);
759 } else if (parent_
) {
760 // Translate the requested paint rect to the parent's coordinate system
761 // then pass this notification up to the parent.
762 parent_
->SchedulePaintInRect(ConvertRectToParent(rect
));
766 void View::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
767 // The cull_set may allow us to skip painting without canvas construction or
768 // even canvas rect intersection.
769 if (cull_set
.ShouldPaint(this)) {
770 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
772 gfx::ScopedCanvas
scoped_canvas(canvas
);
774 // Paint this View and its children, setting the clip rect to the bounds
775 // of this View and translating the origin to the local bounds' top left
778 // Note that the X (or left) position we pass to ClipRectInt takes into
779 // consideration whether or not the view uses a right-to-left layout so that
780 // we paint our view in its mirrored position if need be.
781 gfx::Rect clip_rect
= bounds();
782 clip_rect
.Inset(clip_insets_
);
784 clip_rect
.set_x(parent_
->GetMirroredXForRect(clip_rect
));
785 canvas
->ClipRect(clip_rect
);
786 if (canvas
->IsClipEmpty())
789 // Non-empty clip, translate the graphics such that 0,0 corresponds to where
790 // this view is located (related to its parent).
791 canvas
->Translate(GetMirroredPosition().OffsetFromOrigin());
792 canvas
->Transform(GetTransform());
794 // If we are a paint root, we need to construct our own CullSet object for
795 // propagation to our children.
798 bounds_tree_
.reset(new BoundsTree(2, 5));
800 // Recompute our bounds tree as needed.
801 UpdateRootBounds(bounds_tree_
.get(), gfx::Vector2d());
803 // Grab the clip rect from the supplied canvas to use as the query rect.
804 gfx::Rect canvas_bounds
;
805 if (!canvas
->GetClipBounds(&canvas_bounds
)) {
806 NOTREACHED() << "Failed to get clip bounds from the canvas!";
810 // Now query our bounds_tree_ for a set of damaged views that intersect
811 // our canvas bounds.
812 scoped_ptr
<base::hash_set
<intptr_t> > damaged_views(
813 new base::hash_set
<intptr_t>());
814 bounds_tree_
->AppendIntersectingRecords(
815 canvas_bounds
, damaged_views
.get());
816 // Construct a CullSet to wrap the damaged views set, it will delete it
817 // for us on scope exit.
818 CullSet
paint_root_cull_set(damaged_views
.Pass());
819 // Paint all descendents using our new cull set.
820 PaintCommon(canvas
, paint_root_cull_set
);
822 // Not a paint root, so we can proceed as normal.
823 PaintCommon(canvas
, cull_set
);
828 void View::set_background(Background
* b
) {
829 background_
.reset(b
);
832 void View::SetBorder(scoped_ptr
<Border
> b
) { border_
= b
.Pass(); }
834 ui::ThemeProvider
* View::GetThemeProvider() const {
835 const Widget
* widget
= GetWidget();
836 return widget
? widget
->GetThemeProvider() : NULL
;
839 const ui::NativeTheme
* View::GetNativeTheme() const {
840 const Widget
* widget
= GetWidget();
841 return widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
844 // Input -----------------------------------------------------------------------
846 View
* View::GetEventHandlerForPoint(const gfx::Point
& point
) {
847 return GetEventHandlerForRect(gfx::Rect(point
, gfx::Size(1, 1)));
850 View
* View::GetEventHandlerForRect(const gfx::Rect
& rect
) {
851 return GetEffectiveViewTargeter()->TargetForRect(this, rect
);
854 bool View::CanProcessEventsWithinSubtree() const {
858 View
* View::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
859 // TODO(tdanderson): Move this implementation into ViewTargetDelegate.
860 if (!HitTestPoint(point
) || !CanProcessEventsWithinSubtree())
863 // Walk the child Views recursively looking for the View that most
864 // tightly encloses the specified point.
865 for (int i
= child_count() - 1; i
>= 0; --i
) {
866 View
* child
= child_at(i
);
867 if (!child
->visible())
870 gfx::Point
point_in_child_coords(point
);
871 ConvertPointToTarget(this, child
, &point_in_child_coords
);
872 View
* handler
= child
->GetTooltipHandlerForPoint(point_in_child_coords
);
879 gfx::NativeCursor
View::GetCursor(const ui::MouseEvent
& event
) {
881 static ui::Cursor arrow
;
882 if (!arrow
.platform())
883 arrow
.SetPlatformCursor(LoadCursor(NULL
, IDC_ARROW
));
886 return gfx::kNullCursor
;
890 bool View::HitTestPoint(const gfx::Point
& point
) const {
891 return HitTestRect(gfx::Rect(point
, gfx::Size(1, 1)));
894 bool View::HitTestRect(const gfx::Rect
& rect
) const {
895 return GetEffectiveViewTargeter()->DoesIntersectRect(this, rect
);
898 bool View::IsMouseHovered() {
899 // If we haven't yet been placed in an onscreen view hierarchy, we can't be
904 // If mouse events are disabled, then the mouse cursor is invisible and
905 // is therefore not hovering over this button.
906 if (!GetWidget()->IsMouseEventsEnabled())
909 gfx::Point
cursor_pos(gfx::Screen::GetScreenFor(
910 GetWidget()->GetNativeView())->GetCursorScreenPoint());
911 ConvertPointFromScreen(this, &cursor_pos
);
912 return HitTestPoint(cursor_pos
);
915 bool View::OnMousePressed(const ui::MouseEvent
& event
) {
919 bool View::OnMouseDragged(const ui::MouseEvent
& event
) {
923 void View::OnMouseReleased(const ui::MouseEvent
& event
) {
926 void View::OnMouseCaptureLost() {
929 void View::OnMouseMoved(const ui::MouseEvent
& event
) {
932 void View::OnMouseEntered(const ui::MouseEvent
& event
) {
935 void View::OnMouseExited(const ui::MouseEvent
& event
) {
938 void View::SetMouseHandler(View
* new_mouse_handler
) {
939 // |new_mouse_handler| may be NULL.
941 parent_
->SetMouseHandler(new_mouse_handler
);
944 bool View::OnKeyPressed(const ui::KeyEvent
& event
) {
948 bool View::OnKeyReleased(const ui::KeyEvent
& event
) {
952 bool View::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
956 void View::OnKeyEvent(ui::KeyEvent
* event
) {
957 bool consumed
= (event
->type() == ui::ET_KEY_PRESSED
) ? OnKeyPressed(*event
) :
958 OnKeyReleased(*event
);
960 event
->StopPropagation();
963 void View::OnMouseEvent(ui::MouseEvent
* event
) {
964 switch (event
->type()) {
965 case ui::ET_MOUSE_PRESSED
:
966 if (ProcessMousePressed(*event
))
970 case ui::ET_MOUSE_MOVED
:
971 if ((event
->flags() & (ui::EF_LEFT_MOUSE_BUTTON
|
972 ui::EF_RIGHT_MOUSE_BUTTON
|
973 ui::EF_MIDDLE_MOUSE_BUTTON
)) == 0) {
974 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 make_scoped_ptr(new ui::EventTargetIteratorImpl
<View
>(children_
));
1059 ui::EventTargeter
* View::GetEventTargeter() {
1060 return targeter_
.get();
1063 void View::ConvertEventToTarget(ui::EventTarget
* target
,
1064 ui::LocatedEvent
* event
) {
1065 event
->ConvertLocationToTarget(this, static_cast<View
*>(target
));
1068 // Accelerators ----------------------------------------------------------------
1070 void View::AddAccelerator(const ui::Accelerator
& accelerator
) {
1071 if (!accelerators_
.get())
1072 accelerators_
.reset(new std::vector
<ui::Accelerator
>());
1074 if (std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
) ==
1075 accelerators_
->end()) {
1076 accelerators_
->push_back(accelerator
);
1078 RegisterPendingAccelerators();
1081 void View::RemoveAccelerator(const ui::Accelerator
& accelerator
) {
1082 if (!accelerators_
.get()) {
1083 NOTREACHED() << "Removing non-existing accelerator";
1087 std::vector
<ui::Accelerator
>::iterator
i(
1088 std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
));
1089 if (i
== accelerators_
->end()) {
1090 NOTREACHED() << "Removing non-existing accelerator";
1094 size_t index
= i
- accelerators_
->begin();
1095 accelerators_
->erase(i
);
1096 if (index
>= registered_accelerator_count_
) {
1097 // The accelerator is not registered to FocusManager.
1100 --registered_accelerator_count_
;
1102 // Providing we are attached to a Widget and registered with a focus manager,
1103 // we should de-register from that focus manager now.
1104 if (GetWidget() && accelerator_focus_manager_
)
1105 accelerator_focus_manager_
->UnregisterAccelerator(accelerator
, this);
1108 void View::ResetAccelerators() {
1109 if (accelerators_
.get())
1110 UnregisterAccelerators(false);
1113 bool View::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1117 bool View::CanHandleAccelerators() const {
1118 return enabled() && IsDrawn() && GetWidget() && GetWidget()->IsVisible();
1121 // Focus -----------------------------------------------------------------------
1123 bool View::HasFocus() const {
1124 const FocusManager
* focus_manager
= GetFocusManager();
1125 return focus_manager
&& (focus_manager
->GetFocusedView() == this);
1128 View
* View::GetNextFocusableView() {
1129 return next_focusable_view_
;
1132 const View
* View::GetNextFocusableView() const {
1133 return next_focusable_view_
;
1136 View
* View::GetPreviousFocusableView() {
1137 return previous_focusable_view_
;
1140 void View::SetNextFocusableView(View
* view
) {
1142 view
->previous_focusable_view_
= this;
1143 next_focusable_view_
= view
;
1146 void View::SetFocusable(bool focusable
) {
1147 if (focusable_
== focusable
)
1150 focusable_
= focusable
;
1151 AdvanceFocusIfNecessary();
1154 bool View::IsFocusable() const {
1155 return focusable_
&& enabled_
&& IsDrawn();
1158 bool View::IsAccessibilityFocusable() const {
1159 return (focusable_
|| accessibility_focusable_
) && enabled_
&& IsDrawn();
1162 void View::SetAccessibilityFocusable(bool accessibility_focusable
) {
1163 if (accessibility_focusable_
== accessibility_focusable
)
1166 accessibility_focusable_
= accessibility_focusable
;
1167 AdvanceFocusIfNecessary();
1170 FocusManager
* View::GetFocusManager() {
1171 Widget
* widget
= GetWidget();
1172 return widget
? widget
->GetFocusManager() : NULL
;
1175 const FocusManager
* View::GetFocusManager() const {
1176 const Widget
* widget
= GetWidget();
1177 return widget
? widget
->GetFocusManager() : NULL
;
1180 void View::RequestFocus() {
1181 FocusManager
* focus_manager
= GetFocusManager();
1182 if (focus_manager
&& IsFocusable())
1183 focus_manager
->SetFocusedView(this);
1186 bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) {
1190 FocusTraversable
* View::GetFocusTraversable() {
1194 FocusTraversable
* View::GetPaneFocusTraversable() {
1198 // Tooltips --------------------------------------------------------------------
1200 bool View::GetTooltipText(const gfx::Point
& p
, base::string16
* tooltip
) const {
1204 bool View::GetTooltipTextOrigin(const gfx::Point
& p
, gfx::Point
* loc
) const {
1208 // Context menus ---------------------------------------------------------------
1210 void View::ShowContextMenu(const gfx::Point
& p
,
1211 ui::MenuSourceType source_type
) {
1212 if (!context_menu_controller_
)
1215 context_menu_controller_
->ShowContextMenuForView(this, p
, source_type
);
1219 bool View::ShouldShowContextMenuOnMousePress() {
1220 return kContextMenuOnMousePress
;
1223 // Drag and drop ---------------------------------------------------------------
1225 bool View::GetDropFormats(
1227 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
1231 bool View::AreDropTypesRequired() {
1235 bool View::CanDrop(const OSExchangeData
& data
) {
1236 // TODO(sky): when I finish up migration, this should default to true.
1240 void View::OnDragEntered(const ui::DropTargetEvent
& event
) {
1243 int View::OnDragUpdated(const ui::DropTargetEvent
& event
) {
1244 return ui::DragDropTypes::DRAG_NONE
;
1247 void View::OnDragExited() {
1250 int View::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1251 return ui::DragDropTypes::DRAG_NONE
;
1254 void View::OnDragDone() {
1258 bool View::ExceededDragThreshold(const gfx::Vector2d
& delta
) {
1259 return (abs(delta
.x()) > GetHorizontalDragThreshold() ||
1260 abs(delta
.y()) > GetVerticalDragThreshold());
1263 // Accessibility----------------------------------------------------------------
1265 gfx::NativeViewAccessible
View::GetNativeViewAccessible() {
1266 if (!native_view_accessibility_
)
1267 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1268 if (native_view_accessibility_
)
1269 return native_view_accessibility_
->GetNativeObject();
1273 void View::NotifyAccessibilityEvent(
1274 ui::AXEvent event_type
,
1275 bool send_native_event
) {
1276 if (ViewsDelegate::views_delegate
)
1277 ViewsDelegate::views_delegate
->NotifyAccessibilityEvent(this, event_type
);
1279 if (send_native_event
&& GetWidget()) {
1280 if (!native_view_accessibility_
)
1281 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1282 if (native_view_accessibility_
)
1283 native_view_accessibility_
->NotifyAccessibilityEvent(event_type
);
1287 // Scrolling -------------------------------------------------------------------
1289 void View::ScrollRectToVisible(const gfx::Rect
& rect
) {
1290 // We must take RTL UI mirroring into account when adjusting the position of
1293 gfx::Rect
scroll_rect(rect
);
1294 scroll_rect
.Offset(GetMirroredX(), y());
1295 parent_
->ScrollRectToVisible(scroll_rect
);
1299 int View::GetPageScrollIncrement(ScrollView
* scroll_view
,
1300 bool is_horizontal
, bool is_positive
) {
1304 int View::GetLineScrollIncrement(ScrollView
* scroll_view
,
1305 bool is_horizontal
, bool is_positive
) {
1309 ////////////////////////////////////////////////////////////////////////////////
1312 // Size and disposition --------------------------------------------------------
1314 void View::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1317 void View::PreferredSizeChanged() {
1320 parent_
->ChildPreferredSizeChanged(this);
1323 bool View::GetNeedsNotificationWhenVisibleBoundsChange() const {
1327 void View::OnVisibleBoundsChanged() {
1330 // Tree operations -------------------------------------------------------------
1332 void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails
& details
) {
1335 void View::VisibilityChanged(View
* starting_from
, bool is_visible
) {
1338 void View::NativeViewHierarchyChanged() {
1339 FocusManager
* focus_manager
= GetFocusManager();
1340 if (accelerator_focus_manager_
!= focus_manager
) {
1341 UnregisterAccelerators(true);
1344 RegisterPendingAccelerators();
1348 // Painting --------------------------------------------------------------------
1350 void View::PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1351 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1352 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1353 if (!child_at(i
)->layer())
1354 child_at(i
)->Paint(canvas
, cull_set
);
1357 void View::OnPaint(gfx::Canvas
* canvas
) {
1358 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1359 OnPaintBackground(canvas
);
1360 OnPaintBorder(canvas
);
1363 void View::OnPaintBackground(gfx::Canvas
* canvas
) {
1364 if (background_
.get()) {
1365 TRACE_EVENT2("views", "View::OnPaintBackground",
1366 "width", canvas
->sk_canvas()->getDevice()->width(),
1367 "height", canvas
->sk_canvas()->getDevice()->height());
1368 background_
->Paint(canvas
, this);
1372 void View::OnPaintBorder(gfx::Canvas
* canvas
) {
1373 if (border_
.get()) {
1374 TRACE_EVENT2("views", "View::OnPaintBorder",
1375 "width", canvas
->sk_canvas()->getDevice()->width(),
1376 "height", canvas
->sk_canvas()->getDevice()->height());
1377 border_
->Paint(*this, canvas
);
1381 bool View::IsPaintRoot() {
1382 return paint_to_layer_
|| !parent_
;
1385 // Accelerated Painting --------------------------------------------------------
1387 void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely
) {
1388 // This method should not have the side-effect of creating the layer.
1390 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely
);
1393 gfx::Vector2d
View::CalculateOffsetToAncestorWithLayer(
1394 ui::Layer
** layer_parent
) {
1397 *layer_parent
= layer();
1398 return gfx::Vector2d();
1401 return gfx::Vector2d();
1403 return gfx::Vector2d(GetMirroredX(), y()) +
1404 parent_
->CalculateOffsetToAncestorWithLayer(layer_parent
);
1407 void View::UpdateParentLayer() {
1411 ui::Layer
* parent_layer
= NULL
;
1412 gfx::Vector2d
offset(GetMirroredX(), y());
1415 offset
+= parent_
->CalculateOffsetToAncestorWithLayer(&parent_layer
);
1417 ReparentLayer(offset
, parent_layer
);
1420 void View::MoveLayerToParent(ui::Layer
* parent_layer
,
1421 const gfx::Point
& point
) {
1422 gfx::Point
local_point(point
);
1423 if (parent_layer
!= layer())
1424 local_point
.Offset(GetMirroredX(), y());
1425 if (layer() && parent_layer
!= layer()) {
1426 parent_layer
->Add(layer());
1427 SetLayerBounds(gfx::Rect(local_point
.x(), local_point
.y(),
1428 width(), height()));
1430 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1431 child_at(i
)->MoveLayerToParent(parent_layer
, local_point
);
1435 void View::UpdateLayerVisibility() {
1436 bool visible
= visible_
;
1437 for (const View
* v
= parent_
; visible
&& v
&& !v
->layer(); v
= v
->parent_
)
1438 visible
= v
->visible();
1440 UpdateChildLayerVisibility(visible
);
1443 void View::UpdateChildLayerVisibility(bool ancestor_visible
) {
1445 layer()->SetVisible(ancestor_visible
&& visible_
);
1447 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1448 child_at(i
)->UpdateChildLayerVisibility(ancestor_visible
&& visible_
);
1452 void View::UpdateChildLayerBounds(const gfx::Vector2d
& offset
) {
1454 SetLayerBounds(GetLocalBounds() + offset
);
1456 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
1457 View
* child
= child_at(i
);
1458 child
->UpdateChildLayerBounds(
1459 offset
+ gfx::Vector2d(child
->GetMirroredX(), child
->y()));
1464 void View::OnPaintLayer(gfx::Canvas
* canvas
) {
1465 if (!layer() || !layer()->fills_bounds_opaquely())
1466 canvas
->DrawColor(SK_ColorBLACK
, SkXfermode::kClear_Mode
);
1467 PaintCommon(canvas
, CullSet());
1470 void View::OnDelegatedFrameDamage(
1471 const gfx::Rect
& damage_rect_in_dip
) {
1474 void View::OnDeviceScaleFactorChanged(float device_scale_factor
) {
1475 snap_layer_to_pixel_boundary_
=
1476 (device_scale_factor
- std::floor(device_scale_factor
)) != 0.0f
;
1477 SnapLayerToPixelBoundary();
1478 // Repainting with new scale factor will paint the content at the right scale.
1481 base::Closure
View::PrepareForLayerBoundsChange() {
1482 return base::Closure();
1485 void View::ReorderLayers() {
1487 while (v
&& !v
->layer())
1490 Widget
* widget
= GetWidget();
1493 ui::Layer
* layer
= widget
->GetLayer();
1495 widget
->GetRootView()->ReorderChildLayers(layer
);
1498 v
->ReorderChildLayers(v
->layer());
1502 // Reorder the widget's child NativeViews in case a child NativeView is
1503 // associated with a view (eg via a NativeViewHost). Always do the
1504 // reordering because the associated NativeView's layer (if it has one)
1505 // is parented to the widget's layer regardless of whether the host view has
1506 // an ancestor with a layer.
1507 widget
->ReorderNativeViews();
1511 void View::ReorderChildLayers(ui::Layer
* parent_layer
) {
1512 if (layer() && layer() != parent_layer
) {
1513 DCHECK_EQ(parent_layer
, layer()->parent());
1514 parent_layer
->StackAtBottom(layer());
1516 // Iterate backwards through the children so that a child with a layer
1517 // which is further to the back is stacked above one which is further to
1519 for (Views::reverse_iterator
it(children_
.rbegin());
1520 it
!= children_
.rend(); ++it
) {
1521 (*it
)->ReorderChildLayers(parent_layer
);
1526 // Input -----------------------------------------------------------------------
1528 View::DragInfo
* View::GetDragInfo() {
1529 return parent_
? parent_
->GetDragInfo() : NULL
;
1532 // Focus -----------------------------------------------------------------------
1534 void View::OnFocus() {
1535 // TODO(beng): Investigate whether it's possible for us to move this to
1537 // By default, we clear the native focus. This ensures that no visible native
1538 // view as the focus and that we still receive keyboard inputs.
1539 FocusManager
* focus_manager
= GetFocusManager();
1541 focus_manager
->ClearNativeFocus();
1543 // TODO(beng): Investigate whether it's possible for us to move this to
1545 // Notify assistive technologies of the focus change.
1546 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
1549 void View::OnBlur() {
1552 void View::Focus() {
1560 // Tooltips --------------------------------------------------------------------
1562 void View::TooltipTextChanged() {
1563 Widget
* widget
= GetWidget();
1564 // TooltipManager may be null if there is a problem creating it.
1565 if (widget
&& widget
->GetTooltipManager())
1566 widget
->GetTooltipManager()->TooltipTextChanged(this);
1569 // Context menus ---------------------------------------------------------------
1571 gfx::Point
View::GetKeyboardContextMenuLocation() {
1572 gfx::Rect vis_bounds
= GetVisibleBounds();
1573 gfx::Point
screen_point(vis_bounds
.x() + vis_bounds
.width() / 2,
1574 vis_bounds
.y() + vis_bounds
.height() / 2);
1575 ConvertPointToScreen(this, &screen_point
);
1576 return screen_point
;
1579 // Drag and drop ---------------------------------------------------------------
1581 int View::GetDragOperations(const gfx::Point
& press_pt
) {
1582 return drag_controller_
?
1583 drag_controller_
->GetDragOperationsForView(this, press_pt
) :
1584 ui::DragDropTypes::DRAG_NONE
;
1587 void View::WriteDragData(const gfx::Point
& press_pt
, OSExchangeData
* data
) {
1588 DCHECK(drag_controller_
);
1589 drag_controller_
->WriteDragDataForView(this, press_pt
, data
);
1592 bool View::InDrag() {
1593 Widget
* widget
= GetWidget();
1594 return widget
? widget
->dragged_view() == this : false;
1597 int View::GetHorizontalDragThreshold() {
1598 // TODO(jennyz): This value may need to be adjusted for different platforms
1599 // and for different display density.
1600 return kDefaultHorizontalDragThreshold
;
1603 int View::GetVerticalDragThreshold() {
1604 // TODO(jennyz): This value may need to be adjusted for different platforms
1605 // and for different display density.
1606 return kDefaultVerticalDragThreshold
;
1609 // Debugging -------------------------------------------------------------------
1611 #if !defined(NDEBUG)
1613 std::string
View::PrintViewGraph(bool first
) {
1614 return DoPrintViewGraph(first
, this);
1617 std::string
View::DoPrintViewGraph(bool first
, View
* view_with_children
) {
1618 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1619 const size_t kMaxPointerStringLength
= 19;
1624 result
.append("digraph {\n");
1626 // Node characteristics.
1627 char p
[kMaxPointerStringLength
];
1629 const std::string
class_name(GetClassName());
1630 size_t base_name_index
= class_name
.find_last_of('/');
1631 if (base_name_index
== std::string::npos
)
1632 base_name_index
= 0;
1636 char bounds_buffer
[512];
1638 // Information about current node.
1639 base::snprintf(p
, arraysize(bounds_buffer
), "%p", view_with_children
);
1640 result
.append(" N");
1641 result
.append(p
+ 2);
1642 result
.append(" [label=\"");
1644 result
.append(class_name
.substr(base_name_index
).c_str());
1646 base::snprintf(bounds_buffer
,
1647 arraysize(bounds_buffer
),
1648 "\\n bounds: (%d, %d), (%dx%d)",
1653 result
.append(bounds_buffer
);
1655 gfx::DecomposedTransform decomp
;
1656 if (!GetTransform().IsIdentity() &&
1657 gfx::DecomposeTransform(&decomp
, GetTransform())) {
1658 base::snprintf(bounds_buffer
,
1659 arraysize(bounds_buffer
),
1660 "\\n translation: (%f, %f)",
1661 decomp
.translate
[0],
1662 decomp
.translate
[1]);
1663 result
.append(bounds_buffer
);
1665 base::snprintf(bounds_buffer
,
1666 arraysize(bounds_buffer
),
1667 "\\n rotation: %3.2f",
1668 std::acos(decomp
.quaternion
[3]) * 360.0 / M_PI
);
1669 result
.append(bounds_buffer
);
1671 base::snprintf(bounds_buffer
,
1672 arraysize(bounds_buffer
),
1673 "\\n scale: (%2.4f, %2.4f)",
1676 result
.append(bounds_buffer
);
1679 result
.append("\"");
1681 result
.append(", shape=box");
1683 if (layer()->has_external_content())
1684 result
.append(", color=green");
1686 result
.append(", color=red");
1688 if (layer()->fills_bounds_opaquely())
1689 result
.append(", style=filled");
1691 result
.append("]\n");
1695 char pp
[kMaxPointerStringLength
];
1697 base::snprintf(pp
, kMaxPointerStringLength
, "%p", parent_
);
1698 result
.append(" N");
1699 result
.append(pp
+ 2);
1700 result
.append(" -> N");
1701 result
.append(p
+ 2);
1702 result
.append("\n");
1706 for (int i
= 0, count
= view_with_children
->child_count(); i
< count
; ++i
)
1707 result
.append(view_with_children
->child_at(i
)->PrintViewGraph(false));
1710 result
.append("}\n");
1716 ////////////////////////////////////////////////////////////////////////////////
1719 // DropInfo --------------------------------------------------------------------
1721 void View::DragInfo::Reset() {
1722 possible_drag
= false;
1723 start_pt
= gfx::Point();
1726 void View::DragInfo::PossibleDrag(const gfx::Point
& p
) {
1727 possible_drag
= true;
1731 // Painting --------------------------------------------------------------------
1733 void View::SchedulePaintBoundsChanged(SchedulePaintType type
) {
1734 // If we have a layer and the View's size did not change, we do not need to
1735 // schedule any paints since the layer will be redrawn at its new location
1736 // during the next Draw() cycle in the compositor.
1737 if (!layer() || type
== SCHEDULE_PAINT_SIZE_CHANGED
) {
1738 // Otherwise, if the size changes or we don't have a layer then we need to
1739 // use SchedulePaint to invalidate the area occupied by the View.
1741 } else if (parent_
&& type
== SCHEDULE_PAINT_SIZE_SAME
) {
1742 // The compositor doesn't Draw() until something on screen changes, so
1743 // if our position changes but nothing is being animated on screen, then
1744 // tell the compositor to redraw the scene. We know layer() exists due to
1745 // the above if clause.
1746 layer()->ScheduleDraw();
1750 void View::PaintCommon(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1755 // If the View we are about to paint requested the canvas to be flipped, we
1756 // should change the transform appropriately.
1757 // The canvas mirroring is undone once the View is done painting so that we
1758 // don't pass the canvas with the mirrored transform to Views that didn't
1759 // request the canvas to be flipped.
1760 gfx::ScopedCanvas
scoped(canvas
);
1761 if (FlipCanvasOnPaintForRTLUI()) {
1762 canvas
->Translate(gfx::Vector2d(width(), 0));
1763 canvas
->Scale(-1, 1);
1769 PaintChildren(canvas
, cull_set
);
1772 // Tree operations -------------------------------------------------------------
1774 void View::DoRemoveChildView(View
* view
,
1775 bool update_focus_cycle
,
1776 bool update_tool_tip
,
1777 bool delete_removed_view
,
1781 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
1782 scoped_ptr
<View
> view_to_be_deleted
;
1783 if (i
!= children_
.end()) {
1784 if (update_focus_cycle
) {
1785 // Let's remove the view from the focus traversal.
1786 View
* next_focusable
= view
->next_focusable_view_
;
1787 View
* prev_focusable
= view
->previous_focusable_view_
;
1789 prev_focusable
->next_focusable_view_
= next_focusable
;
1791 next_focusable
->previous_focusable_view_
= prev_focusable
;
1795 UnregisterChildrenForVisibleBoundsNotification(view
);
1796 if (view
->visible())
1797 view
->SchedulePaint();
1798 GetWidget()->NotifyWillRemoveView(view
);
1801 // Remove the bounds of this child and any of its descendants from our
1802 // paint root bounds tree.
1803 BoundsTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
1805 view
->RemoveRootBounds(bounds_tree
);
1807 view
->PropagateRemoveNotifications(this, new_parent
);
1808 view
->parent_
= NULL
;
1809 view
->UpdateLayerVisibility();
1811 if (delete_removed_view
&& !view
->owned_by_client_
)
1812 view_to_be_deleted
.reset(view
);
1817 if (update_tool_tip
)
1820 if (layout_manager_
.get())
1821 layout_manager_
->ViewRemoved(this, view
);
1824 void View::PropagateRemoveNotifications(View
* old_parent
, View
* new_parent
) {
1825 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1826 child_at(i
)->PropagateRemoveNotifications(old_parent
, new_parent
);
1828 ViewHierarchyChangedDetails
details(false, old_parent
, this, new_parent
);
1829 for (View
* v
= this; v
; v
= v
->parent_
)
1830 v
->ViewHierarchyChangedImpl(true, details
);
1833 void View::PropagateAddNotifications(
1834 const ViewHierarchyChangedDetails
& details
) {
1835 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1836 child_at(i
)->PropagateAddNotifications(details
);
1837 ViewHierarchyChangedImpl(true, details
);
1840 void View::PropagateNativeViewHierarchyChanged() {
1841 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1842 child_at(i
)->PropagateNativeViewHierarchyChanged();
1843 NativeViewHierarchyChanged();
1846 void View::ViewHierarchyChangedImpl(
1847 bool register_accelerators
,
1848 const ViewHierarchyChangedDetails
& details
) {
1849 if (register_accelerators
) {
1850 if (details
.is_add
) {
1851 // If you get this registration, you are part of a subtree that has been
1852 // added to the view hierarchy.
1853 if (GetFocusManager())
1854 RegisterPendingAccelerators();
1856 if (details
.child
== this)
1857 UnregisterAccelerators(true);
1861 if (details
.is_add
&& layer() && !layer()->parent()) {
1862 UpdateParentLayer();
1863 Widget
* widget
= GetWidget();
1865 widget
->UpdateRootLayers();
1866 } else if (!details
.is_add
&& details
.child
== this) {
1867 // Make sure the layers belonging to the subtree rooted at |child| get
1868 // removed from layers that do not belong in the same subtree.
1870 Widget
* widget
= GetWidget();
1872 widget
->UpdateRootLayers();
1875 ViewHierarchyChanged(details
);
1876 details
.parent
->needs_layout_
= true;
1879 void View::PropagateNativeThemeChanged(const ui::NativeTheme
* theme
) {
1880 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1881 child_at(i
)->PropagateNativeThemeChanged(theme
);
1882 OnNativeThemeChanged(theme
);
1885 // Size and disposition --------------------------------------------------------
1887 void View::PropagateVisibilityNotifications(View
* start
, bool is_visible
) {
1888 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1889 child_at(i
)->PropagateVisibilityNotifications(start
, is_visible
);
1890 VisibilityChangedImpl(start
, is_visible
);
1893 void View::VisibilityChangedImpl(View
* starting_from
, bool is_visible
) {
1894 VisibilityChanged(starting_from
, is_visible
);
1897 void View::BoundsChanged(const gfx::Rect
& previous_bounds
) {
1898 // Mark our bounds as dirty for the paint root, also see if we need to
1899 // recompute our children's bounds due to origin change.
1900 bool origin_changed
=
1901 previous_bounds
.OffsetFromOrigin() != bounds_
.OffsetFromOrigin();
1902 SetRootBoundsDirty(origin_changed
);
1905 // Paint the new bounds.
1906 SchedulePaintBoundsChanged(
1907 bounds_
.size() == previous_bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
1908 SCHEDULE_PAINT_SIZE_CHANGED
);
1913 SetLayerBounds(GetLocalBounds() +
1914 gfx::Vector2d(GetMirroredX(), y()) +
1915 parent_
->CalculateOffsetToAncestorWithLayer(NULL
));
1917 SetLayerBounds(bounds_
);
1920 // In RTL mode, if our width has changed, our children's mirrored bounds
1921 // will have changed. Update the child's layer bounds, or if it is not a
1922 // layer, the bounds of any layers inside the child.
1923 if (base::i18n::IsRTL() && bounds_
.width() != previous_bounds
.width()) {
1924 for (int i
= 0; i
< child_count(); ++i
) {
1925 View
* child
= child_at(i
);
1926 child
->UpdateChildLayerBounds(
1927 gfx::Vector2d(child
->GetMirroredX(), child
->y()));
1931 // If our bounds have changed, then any descendant layer bounds may have
1932 // changed. Update them accordingly.
1933 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
1936 OnBoundsChanged(previous_bounds
);
1938 if (previous_bounds
.size() != size()) {
1939 needs_layout_
= false;
1943 if (GetNeedsNotificationWhenVisibleBoundsChange())
1944 OnVisibleBoundsChanged();
1946 // Notify interested Views that visible bounds within the root view may have
1948 if (descendants_to_notify_
.get()) {
1949 for (Views::iterator
i(descendants_to_notify_
->begin());
1950 i
!= descendants_to_notify_
->end(); ++i
) {
1951 (*i
)->OnVisibleBoundsChanged();
1957 void View::RegisterChildrenForVisibleBoundsNotification(View
* view
) {
1958 if (view
->GetNeedsNotificationWhenVisibleBoundsChange())
1959 view
->RegisterForVisibleBoundsNotification();
1960 for (int i
= 0; i
< view
->child_count(); ++i
)
1961 RegisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
1965 void View::UnregisterChildrenForVisibleBoundsNotification(View
* view
) {
1966 if (view
->GetNeedsNotificationWhenVisibleBoundsChange())
1967 view
->UnregisterForVisibleBoundsNotification();
1968 for (int i
= 0; i
< view
->child_count(); ++i
)
1969 UnregisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
1972 void View::RegisterForVisibleBoundsNotification() {
1973 if (registered_for_visible_bounds_notification_
)
1976 registered_for_visible_bounds_notification_
= true;
1977 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
1978 ancestor
->AddDescendantToNotify(this);
1981 void View::UnregisterForVisibleBoundsNotification() {
1982 if (!registered_for_visible_bounds_notification_
)
1985 registered_for_visible_bounds_notification_
= false;
1986 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
1987 ancestor
->RemoveDescendantToNotify(this);
1990 void View::AddDescendantToNotify(View
* view
) {
1992 if (!descendants_to_notify_
.get())
1993 descendants_to_notify_
.reset(new Views
);
1994 descendants_to_notify_
->push_back(view
);
1997 void View::RemoveDescendantToNotify(View
* view
) {
1998 DCHECK(view
&& descendants_to_notify_
.get());
1999 Views::iterator
i(std::find(
2000 descendants_to_notify_
->begin(), descendants_to_notify_
->end(), view
));
2001 DCHECK(i
!= descendants_to_notify_
->end());
2002 descendants_to_notify_
->erase(i
);
2003 if (descendants_to_notify_
->empty())
2004 descendants_to_notify_
.reset();
2007 void View::SetLayerBounds(const gfx::Rect
& bounds
) {
2008 layer()->SetBounds(bounds
);
2009 SnapLayerToPixelBoundary();
2012 void View::SetRootBoundsDirty(bool origin_changed
) {
2013 root_bounds_dirty_
= true;
2015 if (origin_changed
) {
2016 // Inform our children that their root bounds are dirty, as their relative
2017 // coordinates in paint root space have changed since ours have changed.
2018 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end();
2020 if (!(*i
)->IsPaintRoot())
2021 (*i
)->SetRootBoundsDirty(origin_changed
);
2026 void View::UpdateRootBounds(BoundsTree
* tree
, const gfx::Vector2d
& offset
) {
2027 // If we're not visible no need to update BoundsTree. When we are made visible
2028 // the BoundsTree will be updated appropriately.
2032 if (!root_bounds_dirty_
&& children_
.empty())
2035 // No need to recompute bounds if we haven't flagged ours as dirty.
2036 TRACE_EVENT1("views", "View::UpdateRootBounds", "class", GetClassName());
2038 // Add our own offset to the provided offset, for our own bounds update and
2039 // for propagation to our children if needed.
2040 gfx::Vector2d view_offset
= offset
+ GetMirroredBounds().OffsetFromOrigin();
2042 // If our bounds have changed we must re-insert our new bounds to the tree.
2043 if (root_bounds_dirty_
) {
2044 root_bounds_dirty_
= false;
2046 view_offset
.x(), view_offset
.y(), bounds_
.width(), bounds_
.height());
2047 tree
->Insert(bounds
, reinterpret_cast<intptr_t>(this));
2050 // Update our children's bounds if needed.
2051 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2052 // We don't descend in to layer views for bounds recomputation, as they
2053 // manage their own RTree as paint roots.
2054 if (!(*i
)->IsPaintRoot())
2055 (*i
)->UpdateRootBounds(tree
, view_offset
);
2059 void View::RemoveRootBounds(BoundsTree
* tree
) {
2060 tree
->Remove(reinterpret_cast<intptr_t>(this));
2062 root_bounds_dirty_
= true;
2064 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2065 if (!(*i
)->IsPaintRoot())
2066 (*i
)->RemoveRootBounds(tree
);
2070 View::BoundsTree
* View::GetBoundsTreeFromPaintRoot() {
2071 BoundsTree
* bounds_tree
= bounds_tree_
.get();
2072 View
* paint_root
= this;
2073 while (!bounds_tree
&& !paint_root
->IsPaintRoot()) {
2074 // Assumption is that if IsPaintRoot() is false then parent_ is valid.
2076 paint_root
= paint_root
->parent_
;
2077 bounds_tree
= paint_root
->bounds_tree_
.get();
2083 // Transformations -------------------------------------------------------------
2085 bool View::GetTransformRelativeTo(const View
* ancestor
,
2086 gfx::Transform
* transform
) const {
2087 const View
* p
= this;
2089 while (p
&& p
!= ancestor
) {
2090 transform
->ConcatTransform(p
->GetTransform());
2091 gfx::Transform translation
;
2092 translation
.Translate(static_cast<float>(p
->GetMirroredX()),
2093 static_cast<float>(p
->y()));
2094 transform
->ConcatTransform(translation
);
2099 return p
== ancestor
;
2102 // Coordinate conversion -------------------------------------------------------
2104 bool View::ConvertPointForAncestor(const View
* ancestor
,
2105 gfx::Point
* point
) const {
2106 gfx::Transform trans
;
2107 // TODO(sad): Have some way of caching the transformation results.
2108 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2109 gfx::Point3F
p(*point
);
2110 trans
.TransformPoint(&p
);
2111 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2115 bool View::ConvertPointFromAncestor(const View
* ancestor
,
2116 gfx::Point
* point
) const {
2117 gfx::Transform trans
;
2118 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2119 gfx::Point3F
p(*point
);
2120 trans
.TransformPointReverse(&p
);
2121 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2125 bool View::ConvertRectForAncestor(const View
* ancestor
,
2126 gfx::RectF
* rect
) const {
2127 gfx::Transform trans
;
2128 // TODO(sad): Have some way of caching the transformation results.
2129 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2130 trans
.TransformRect(rect
);
2134 bool View::ConvertRectFromAncestor(const View
* ancestor
,
2135 gfx::RectF
* rect
) const {
2136 gfx::Transform trans
;
2137 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2138 trans
.TransformRectReverse(rect
);
2142 // Accelerated painting --------------------------------------------------------
2144 void View::CreateLayer() {
2145 // A new layer is being created for the view. So all the layers of the
2146 // sub-tree can inherit the visibility of the corresponding view.
2147 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2148 child_at(i
)->UpdateChildLayerVisibility(true);
2150 SetLayer(new ui::Layer());
2151 layer()->set_delegate(this);
2152 #if !defined(NDEBUG)
2153 layer()->set_name(GetClassName());
2156 UpdateParentLayers();
2157 UpdateLayerVisibility();
2159 // The new layer needs to be ordered in the layer tree according
2160 // to the view tree. Children of this layer were added in order
2161 // in UpdateParentLayers().
2163 parent()->ReorderLayers();
2165 Widget
* widget
= GetWidget();
2167 widget
->UpdateRootLayers();
2170 void View::UpdateParentLayers() {
2171 // Attach all top-level un-parented layers.
2172 if (layer() && !layer()->parent()) {
2173 UpdateParentLayer();
2175 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2176 child_at(i
)->UpdateParentLayers();
2180 void View::OrphanLayers() {
2182 if (layer()->parent())
2183 layer()->parent()->Remove(layer());
2185 // The layer belonging to this View has already been orphaned. It is not
2186 // necessary to orphan the child layers.
2189 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2190 child_at(i
)->OrphanLayers();
2193 void View::ReparentLayer(const gfx::Vector2d
& offset
, ui::Layer
* parent_layer
) {
2194 layer()->SetBounds(GetLocalBounds() + offset
);
2195 DCHECK_NE(layer(), parent_layer
);
2197 parent_layer
->Add(layer());
2198 layer()->SchedulePaint(GetLocalBounds());
2199 MoveLayerToParent(layer(), gfx::Point());
2202 void View::DestroyLayer() {
2203 ui::Layer
* new_parent
= layer()->parent();
2204 std::vector
<ui::Layer
*> children
= layer()->children();
2205 for (size_t i
= 0; i
< children
.size(); ++i
) {
2206 layer()->Remove(children
[i
]);
2208 new_parent
->Add(children
[i
]);
2211 LayerOwner::DestroyLayer();
2216 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
2220 Widget
* widget
= GetWidget();
2222 widget
->UpdateRootLayers();
2225 // Input -----------------------------------------------------------------------
2227 bool View::ProcessMousePressed(const ui::MouseEvent
& event
) {
2228 int drag_operations
=
2229 (enabled_
&& event
.IsOnlyLeftMouseButton() &&
2230 HitTestPoint(event
.location())) ?
2231 GetDragOperations(event
.location()) : 0;
2232 ContextMenuController
* context_menu_controller
= event
.IsRightMouseButton() ?
2233 context_menu_controller_
: 0;
2234 View::DragInfo
* drag_info
= GetDragInfo();
2236 // TODO(sky): for debugging 360238.
2238 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2239 kContextMenuOnMousePress
&& HitTestPoint(event
.location())) {
2240 ViewStorage
* view_storage
= ViewStorage::GetInstance();
2241 storage_id
= view_storage
->CreateStorageID();
2242 view_storage
->StoreView(storage_id
, this);
2245 const bool enabled
= enabled_
;
2246 const bool result
= OnMousePressed(event
);
2251 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2252 kContextMenuOnMousePress
) {
2253 // Assume that if there is a context menu controller we won't be deleted
2254 // from mouse pressed.
2255 gfx::Point
location(event
.location());
2256 if (HitTestPoint(location
)) {
2257 if (storage_id
!= 0)
2258 CHECK_EQ(this, ViewStorage::GetInstance()->RetrieveView(storage_id
));
2259 ConvertPointToScreen(this, &location
);
2260 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2265 // WARNING: we may have been deleted, don't use any View variables.
2266 if (drag_operations
!= ui::DragDropTypes::DRAG_NONE
) {
2267 drag_info
->PossibleDrag(event
.location());
2270 return !!context_menu_controller
|| result
;
2273 bool View::ProcessMouseDragged(const ui::MouseEvent
& event
) {
2274 // Copy the field, that way if we're deleted after drag and drop no harm is
2276 ContextMenuController
* context_menu_controller
= context_menu_controller_
;
2277 const bool possible_drag
= GetDragInfo()->possible_drag
;
2278 if (possible_drag
&&
2279 ExceededDragThreshold(GetDragInfo()->start_pt
- event
.location()) &&
2280 (!drag_controller_
||
2281 drag_controller_
->CanStartDragForView(
2282 this, GetDragInfo()->start_pt
, event
.location()))) {
2283 DoDrag(event
, GetDragInfo()->start_pt
,
2284 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
);
2286 if (OnMouseDragged(event
))
2288 // Fall through to return value based on context menu controller.
2290 // WARNING: we may have been deleted.
2291 return (context_menu_controller
!= NULL
) || possible_drag
;
2294 void View::ProcessMouseReleased(const ui::MouseEvent
& event
) {
2295 if (!kContextMenuOnMousePress
&& context_menu_controller_
&&
2296 event
.IsOnlyRightMouseButton()) {
2297 // Assume that if there is a context menu controller we won't be deleted
2298 // from mouse released.
2299 gfx::Point
location(event
.location());
2300 OnMouseReleased(event
);
2301 if (HitTestPoint(location
)) {
2302 ConvertPointToScreen(this, &location
);
2303 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2306 OnMouseReleased(event
);
2308 // WARNING: we may have been deleted.
2311 // Accelerators ----------------------------------------------------------------
2313 void View::RegisterPendingAccelerators() {
2314 if (!accelerators_
.get() ||
2315 registered_accelerator_count_
== accelerators_
->size()) {
2316 // No accelerators are waiting for registration.
2321 // The view is not yet attached to a widget, defer registration until then.
2325 accelerator_focus_manager_
= GetFocusManager();
2326 if (!accelerator_focus_manager_
) {
2327 // Some crash reports seem to show that we may get cases where we have no
2328 // focus manager (see bug #1291225). This should never be the case, just
2329 // making sure we don't crash.
2333 for (std::vector
<ui::Accelerator
>::const_iterator
i(
2334 accelerators_
->begin() + registered_accelerator_count_
);
2335 i
!= accelerators_
->end(); ++i
) {
2336 accelerator_focus_manager_
->RegisterAccelerator(
2337 *i
, ui::AcceleratorManager::kNormalPriority
, this);
2339 registered_accelerator_count_
= accelerators_
->size();
2342 void View::UnregisterAccelerators(bool leave_data_intact
) {
2343 if (!accelerators_
.get())
2347 if (accelerator_focus_manager_
) {
2348 accelerator_focus_manager_
->UnregisterAccelerators(this);
2349 accelerator_focus_manager_
= NULL
;
2351 if (!leave_data_intact
) {
2352 accelerators_
->clear();
2353 accelerators_
.reset();
2355 registered_accelerator_count_
= 0;
2359 // Focus -----------------------------------------------------------------------
2361 void View::InitFocusSiblings(View
* v
, int index
) {
2362 int count
= child_count();
2365 v
->next_focusable_view_
= NULL
;
2366 v
->previous_focusable_view_
= NULL
;
2368 if (index
== count
) {
2369 // We are inserting at the end, but the end of the child list may not be
2370 // the last focusable element. Let's try to find an element with no next
2371 // focusable element to link to.
2372 View
* last_focusable_view
= NULL
;
2373 for (Views::iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2374 if (!(*i
)->next_focusable_view_
) {
2375 last_focusable_view
= *i
;
2379 if (last_focusable_view
== NULL
) {
2380 // Hum... there is a cycle in the focus list. Let's just insert ourself
2381 // after the last child.
2382 View
* prev
= children_
[index
- 1];
2383 v
->previous_focusable_view_
= prev
;
2384 v
->next_focusable_view_
= prev
->next_focusable_view_
;
2385 prev
->next_focusable_view_
->previous_focusable_view_
= v
;
2386 prev
->next_focusable_view_
= v
;
2388 last_focusable_view
->next_focusable_view_
= v
;
2389 v
->next_focusable_view_
= NULL
;
2390 v
->previous_focusable_view_
= last_focusable_view
;
2393 View
* prev
= children_
[index
]->GetPreviousFocusableView();
2394 v
->previous_focusable_view_
= prev
;
2395 v
->next_focusable_view_
= children_
[index
];
2397 prev
->next_focusable_view_
= v
;
2398 children_
[index
]->previous_focusable_view_
= v
;
2403 void View::AdvanceFocusIfNecessary() {
2404 // Focus should only be advanced if this is the focused view and has become
2405 // unfocusable. If the view is still focusable or is not focused, we can
2406 // return early avoiding furthur unnecessary checks. Focusability check is
2407 // performed first as it tends to be faster.
2408 if (IsAccessibilityFocusable() || !HasFocus())
2411 FocusManager
* focus_manager
= GetFocusManager();
2413 focus_manager
->AdvanceFocusIfNecessary();
2416 // System events ---------------------------------------------------------------
2418 void View::PropagateThemeChanged() {
2419 for (int i
= child_count() - 1; i
>= 0; --i
)
2420 child_at(i
)->PropagateThemeChanged();
2424 void View::PropagateLocaleChanged() {
2425 for (int i
= child_count() - 1; i
>= 0; --i
)
2426 child_at(i
)->PropagateLocaleChanged();
2430 void View::PropagateDeviceScaleFactorChanged(float device_scale_factor
) {
2431 for (int i
= child_count() - 1; i
>= 0; --i
)
2432 child_at(i
)->PropagateDeviceScaleFactorChanged(device_scale_factor
);
2434 // If the view is drawing to the layer, OnDeviceScaleFactorChanged() is called
2435 // through LayerDelegate callback.
2437 OnDeviceScaleFactorChanged(device_scale_factor
);
2440 // Tooltips --------------------------------------------------------------------
2442 void View::UpdateTooltip() {
2443 Widget
* widget
= GetWidget();
2444 // TODO(beng): The TooltipManager NULL check can be removed when we
2445 // consolidate Init() methods and make views_unittests Init() all
2446 // Widgets that it uses.
2447 if (widget
&& widget
->GetTooltipManager())
2448 widget
->GetTooltipManager()->UpdateTooltip();
2451 // Drag and drop ---------------------------------------------------------------
2453 bool View::DoDrag(const ui::LocatedEvent
& event
,
2454 const gfx::Point
& press_pt
,
2455 ui::DragDropTypes::DragEventSource source
) {
2456 int drag_operations
= GetDragOperations(press_pt
);
2457 if (drag_operations
== ui::DragDropTypes::DRAG_NONE
)
2460 Widget
* widget
= GetWidget();
2461 // We should only start a drag from an event, implying we have a widget.
2464 // Don't attempt to start a drag while in the process of dragging. This is
2465 // especially important on X where we can get multiple mouse move events when
2466 // we start the drag.
2467 if (widget
->dragged_view())
2470 OSExchangeData data
;
2471 WriteDragData(press_pt
, &data
);
2473 // Message the RootView to do the drag and drop. That way if we're removed
2474 // the RootView can detect it and avoid calling us back.
2475 gfx::Point
widget_location(event
.location());
2476 ConvertPointToWidget(this, &widget_location
);
2477 widget
->RunShellDrag(this, data
, widget_location
, drag_operations
, source
);
2478 // WARNING: we may have been deleted.
2482 } // namespace views