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/layer.h"
24 #include "ui/compositor/layer_animator.h"
25 #include "ui/events/event_target_iterator.h"
26 #include "ui/gfx/canvas.h"
27 #include "ui/gfx/interpolated_transform.h"
28 #include "ui/gfx/path.h"
29 #include "ui/gfx/point3_f.h"
30 #include "ui/gfx/point_conversions.h"
31 #include "ui/gfx/rect_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/rect_based_targeting_utils.h"
45 #include "ui/views/views_delegate.h"
46 #include "ui/views/widget/native_widget_private.h"
47 #include "ui/views/widget/root_view.h"
48 #include "ui/views/widget/tooltip_manager.h"
49 #include "ui/views/widget/widget.h"
52 #include "base/win/scoped_gdi_object.h"
58 const bool kContextMenuOnMousePress
= false;
60 const bool kContextMenuOnMousePress
= true;
63 // The minimum percentage of a view's area that needs to be covered by a rect
64 // representing a touch region in order for that view to be considered by the
65 // rect-based targeting algorithm.
66 static const float kRectTargetOverlap
= 0.6f
;
68 // Default horizontal drag threshold in pixels.
69 // Same as what gtk uses.
70 const int kDefaultHorizontalDragThreshold
= 8;
72 // Default vertical drag threshold in pixels.
73 // Same as what gtk uses.
74 const int kDefaultVerticalDragThreshold
= 8;
76 // Returns the top view in |view|'s hierarchy.
77 const views::View
* GetHierarchyRoot(const views::View
* view
) {
78 const views::View
* root
= view
;
79 while (root
&& root
->parent())
80 root
= root
->parent();
90 } // namespace internal
93 ViewsDelegate
* ViewsDelegate::views_delegate
= NULL
;
96 const char View::kViewClassName
[] = "View";
98 ////////////////////////////////////////////////////////////////////////////////
101 // Creation and lifetime -------------------------------------------------------
104 : owned_by_client_(false),
110 notify_enter_exit_on_child_(false),
111 registered_for_visible_bounds_notification_(false),
112 root_bounds_dirty_(true),
113 clip_insets_(0, 0, 0, 0),
115 flip_canvas_on_paint_for_rtl_ui_(false),
116 paint_to_layer_(false),
117 accelerator_focus_manager_(NULL
),
118 registered_accelerator_count_(0),
119 next_focusable_view_(NULL
),
120 previous_focusable_view_(NULL
),
122 accessibility_focusable_(false),
123 context_menu_controller_(NULL
),
124 drag_controller_(NULL
),
125 native_view_accessibility_(NULL
) {
130 parent_
->RemoveChildView(this);
132 ViewStorage::GetInstance()->ViewRemoved(this);
134 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
135 (*i
)->parent_
= NULL
;
136 if (!(*i
)->owned_by_client_
)
140 // Release ownership of the native accessibility object, but it's
141 // reference-counted on some platforms, so it may not be deleted right away.
142 if (native_view_accessibility_
)
143 native_view_accessibility_
->Destroy();
146 // Tree operations -------------------------------------------------------------
148 const Widget
* View::GetWidget() const {
149 // The root view holds a reference to this view hierarchy's Widget.
150 return parent_
? parent_
->GetWidget() : NULL
;
153 Widget
* View::GetWidget() {
154 return const_cast<Widget
*>(const_cast<const View
*>(this)->GetWidget());
157 void View::AddChildView(View
* view
) {
158 if (view
->parent_
== this)
160 AddChildViewAt(view
, child_count());
163 void View::AddChildViewAt(View
* view
, int index
) {
164 CHECK_NE(view
, this) << "You cannot add a view as its own child";
166 DCHECK_LE(index
, child_count());
168 // If |view| has a parent, remove it from its parent.
169 View
* parent
= view
->parent_
;
170 ui::NativeTheme
* old_theme
= NULL
;
172 old_theme
= view
->GetNativeTheme();
173 if (parent
== this) {
174 ReorderChildView(view
, index
);
177 parent
->DoRemoveChildView(view
, true, true, false, this);
180 // Sets the prev/next focus views.
181 InitFocusSiblings(view
, index
);
183 // Let's insert the view.
184 view
->parent_
= this;
185 children_
.insert(children_
.begin() + index
, view
);
187 // Instruct the view to recompute its root bounds on next Paint().
188 view
->SetRootBoundsDirty(true);
190 views::Widget
* widget
= GetWidget();
192 const ui::NativeTheme
* new_theme
= view
->GetNativeTheme();
193 if (new_theme
!= old_theme
)
194 view
->PropagateNativeThemeChanged(new_theme
);
197 ViewHierarchyChangedDetails
details(true, this, view
, parent
);
199 for (View
* v
= this; v
; v
= v
->parent_
)
200 v
->ViewHierarchyChangedImpl(false, details
);
202 view
->PropagateAddNotifications(details
);
205 RegisterChildrenForVisibleBoundsNotification(view
);
207 view
->SchedulePaint();
210 if (layout_manager_
.get())
211 layout_manager_
->ViewAdded(this, view
);
215 // Make sure the visibility of the child layers are correct.
216 // If any of the parent View is hidden, then the layers of the subtree
217 // rooted at |this| should be hidden. Otherwise, all the child layers should
218 // inherit the visibility of the owner View.
219 UpdateLayerVisibility();
222 void View::ReorderChildView(View
* view
, int index
) {
223 DCHECK_EQ(view
->parent_
, this);
225 index
= child_count() - 1;
226 else if (index
>= child_count())
228 if (children_
[index
] == view
)
231 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
232 DCHECK(i
!= children_
.end());
235 // Unlink the view first
236 View
* next_focusable
= view
->next_focusable_view_
;
237 View
* prev_focusable
= view
->previous_focusable_view_
;
239 prev_focusable
->next_focusable_view_
= next_focusable
;
241 next_focusable
->previous_focusable_view_
= prev_focusable
;
243 // Add it in the specified index now.
244 InitFocusSiblings(view
, index
);
245 children_
.insert(children_
.begin() + index
, view
);
250 void View::RemoveChildView(View
* view
) {
251 DoRemoveChildView(view
, true, true, false, NULL
);
254 void View::RemoveAllChildViews(bool delete_children
) {
255 while (!children_
.empty())
256 DoRemoveChildView(children_
.front(), false, false, delete_children
, NULL
);
260 bool View::Contains(const View
* view
) const {
261 for (const View
* v
= view
; v
; v
= v
->parent_
) {
268 int View::GetIndexOf(const View
* view
) const {
269 Views::const_iterator
i(std::find(children_
.begin(), children_
.end(), view
));
270 return i
!= children_
.end() ? static_cast<int>(i
- children_
.begin()) : -1;
273 // Size and disposition --------------------------------------------------------
275 void View::SetBounds(int x
, int y
, int width
, int height
) {
276 SetBoundsRect(gfx::Rect(x
, y
, std::max(0, width
), std::max(0, height
)));
279 void View::SetBoundsRect(const gfx::Rect
& bounds
) {
280 if (bounds
== bounds_
) {
282 needs_layout_
= false;
289 // Paint where the view is currently.
290 SchedulePaintBoundsChanged(
291 bounds_
.size() == bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
292 SCHEDULE_PAINT_SIZE_CHANGED
);
295 gfx::Rect prev
= bounds_
;
300 void View::SetSize(const gfx::Size
& size
) {
301 SetBounds(x(), y(), size
.width(), size
.height());
304 void View::SetPosition(const gfx::Point
& position
) {
305 SetBounds(position
.x(), position
.y(), width(), height());
308 void View::SetX(int x
) {
309 SetBounds(x
, y(), width(), height());
312 void View::SetY(int y
) {
313 SetBounds(x(), y
, width(), height());
316 gfx::Rect
View::GetContentsBounds() const {
317 gfx::Rect
contents_bounds(GetLocalBounds());
319 contents_bounds
.Inset(border_
->GetInsets());
320 return contents_bounds
;
323 gfx::Rect
View::GetLocalBounds() const {
324 return gfx::Rect(size());
327 gfx::Rect
View::GetLayerBoundsInPixel() const {
328 return layer()->GetTargetBounds();
331 gfx::Insets
View::GetInsets() const {
332 return border_
.get() ? border_
->GetInsets() : gfx::Insets();
335 gfx::Rect
View::GetVisibleBounds() const {
338 gfx::Rect
vis_bounds(GetLocalBounds());
339 gfx::Rect ancestor_bounds
;
340 const View
* view
= this;
341 gfx::Transform transform
;
343 while (view
!= NULL
&& !vis_bounds
.IsEmpty()) {
344 transform
.ConcatTransform(view
->GetTransform());
345 gfx::Transform translation
;
346 translation
.Translate(static_cast<float>(view
->GetMirroredX()),
347 static_cast<float>(view
->y()));
348 transform
.ConcatTransform(translation
);
350 vis_bounds
= view
->ConvertRectToParent(vis_bounds
);
351 const View
* ancestor
= view
->parent_
;
352 if (ancestor
!= NULL
) {
353 ancestor_bounds
.SetRect(0, 0, ancestor
->width(), ancestor
->height());
354 vis_bounds
.Intersect(ancestor_bounds
);
355 } else if (!view
->GetWidget()) {
356 // If the view has no Widget, we're not visible. Return an empty rect.
361 if (vis_bounds
.IsEmpty())
363 // Convert back to this views coordinate system.
364 gfx::RectF
views_vis_bounds(vis_bounds
);
365 transform
.TransformRectReverse(&views_vis_bounds
);
366 // Partially visible pixels should be considered visible.
367 return gfx::ToEnclosingRect(views_vis_bounds
);
370 gfx::Rect
View::GetBoundsInScreen() const {
372 View::ConvertPointToScreen(this, &origin
);
373 return gfx::Rect(origin
, size());
376 gfx::Size
View::GetPreferredSize() const {
377 if (layout_manager_
.get())
378 return layout_manager_
->GetPreferredSize(this);
382 int View::GetBaseline() const {
386 void View::SizeToPreferredSize() {
387 gfx::Size prefsize
= GetPreferredSize();
388 if ((prefsize
.width() != width()) || (prefsize
.height() != height()))
389 SetBounds(x(), y(), prefsize
.width(), prefsize
.height());
392 gfx::Size
View::GetMinimumSize() const {
393 return GetPreferredSize();
396 gfx::Size
View::GetMaximumSize() const {
400 int View::GetHeightForWidth(int w
) const {
401 if (layout_manager_
.get())
402 return layout_manager_
->GetPreferredHeightForWidth(this, w
);
403 return GetPreferredSize().height();
406 void View::SetVisible(bool visible
) {
407 if (visible
!= visible_
) {
408 // If the View is currently visible, schedule paint to refresh parent.
409 // TODO(beng): not sure we should be doing this if we have a layer.
415 // Notify the parent.
417 parent_
->ChildVisibilityChanged(this);
419 // This notifies all sub-views recursively.
420 PropagateVisibilityNotifications(this, visible_
);
421 UpdateLayerVisibility();
423 // If we are newly visible, schedule paint.
429 bool View::IsDrawn() const {
430 return visible_
&& parent_
? parent_
->IsDrawn() : false;
433 void View::SetEnabled(bool enabled
) {
434 if (enabled
!= enabled_
) {
440 void View::OnEnabledChanged() {
444 // Transformations -------------------------------------------------------------
446 gfx::Transform
View::GetTransform() const {
447 return layer() ? layer()->transform() : gfx::Transform();
450 void View::SetTransform(const gfx::Transform
& transform
) {
451 if (transform
.IsIdentity()) {
453 layer()->SetTransform(transform
);
454 if (!paint_to_layer_
)
462 layer()->SetTransform(transform
);
463 layer()->ScheduleDraw();
467 void View::SetPaintToLayer(bool paint_to_layer
) {
468 if (paint_to_layer_
== paint_to_layer
)
471 // If this is a change in state we will also need to update bounds trees.
472 if (paint_to_layer
) {
473 // Gaining a layer means becoming a paint root. We must remove ourselves
474 // from our old paint root, if we had one. Traverse up view tree to find old
476 View
* old_paint_root
= parent_
;
477 while (old_paint_root
&& !old_paint_root
->IsPaintRoot())
478 old_paint_root
= old_paint_root
->parent_
;
480 // Remove our and our children's bounds from the old tree. This will also
481 // mark all of our bounds as dirty.
482 if (old_paint_root
&& old_paint_root
->bounds_tree_
)
483 RemoveRootBounds(old_paint_root
->bounds_tree_
.get());
486 // Losing a layer means we are no longer a paint root, so delete our
487 // bounds tree and mark ourselves as dirty for future insertion into our
488 // new paint root's bounds tree.
489 bounds_tree_
.reset();
490 SetRootBoundsDirty(true);
493 paint_to_layer_
= paint_to_layer
;
494 if (paint_to_layer_
&& !layer()) {
496 } else if (!paint_to_layer_
&& layer()) {
501 // RTL positioning -------------------------------------------------------------
503 gfx::Rect
View::GetMirroredBounds() const {
504 gfx::Rect
bounds(bounds_
);
505 bounds
.set_x(GetMirroredX());
509 gfx::Point
View::GetMirroredPosition() const {
510 return gfx::Point(GetMirroredX(), y());
513 int View::GetMirroredX() const {
514 return parent_
? parent_
->GetMirroredXForRect(bounds_
) : x();
517 int View::GetMirroredXForRect(const gfx::Rect
& bounds
) const {
518 return base::i18n::IsRTL() ?
519 (width() - bounds
.x() - bounds
.width()) : bounds
.x();
522 int View::GetMirroredXInView(int x
) const {
523 return base::i18n::IsRTL() ? width() - x
: x
;
526 int View::GetMirroredXWithWidthInView(int x
, int w
) const {
527 return base::i18n::IsRTL() ? width() - x
- w
: x
;
530 // Layout ----------------------------------------------------------------------
532 void View::Layout() {
533 needs_layout_
= false;
535 // If we have a layout manager, let it handle the layout for us.
536 if (layout_manager_
.get())
537 layout_manager_
->Layout(this);
539 // Make sure to propagate the Layout() call to any children that haven't
540 // received it yet through the layout manager and need to be laid out. This
541 // is needed for the case when the child requires a layout but its bounds
542 // weren't changed by the layout manager. If there is no layout manager, we
543 // just propagate the Layout() call down the hierarchy, so whoever receives
544 // the call can take appropriate action.
545 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
546 View
* child
= child_at(i
);
547 if (child
->needs_layout_
|| !layout_manager_
.get()) {
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 // Attributes ------------------------------------------------------------------
577 const char* View::GetClassName() const {
578 return kViewClassName
;
581 const View
* View::GetAncestorWithClassName(const std::string
& name
) const {
582 for (const View
* view
= this; view
; view
= view
->parent_
) {
583 if (!strcmp(view
->GetClassName(), name
.c_str()))
589 View
* View::GetAncestorWithClassName(const std::string
& name
) {
590 return const_cast<View
*>(const_cast<const View
*>(this)->
591 GetAncestorWithClassName(name
));
594 const View
* View::GetViewByID(int id
) const {
596 return const_cast<View
*>(this);
598 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
599 const View
* view
= child_at(i
)->GetViewByID(id
);
606 View
* View::GetViewByID(int id
) {
607 return const_cast<View
*>(const_cast<const View
*>(this)->GetViewByID(id
));
610 void View::SetGroup(int gid
) {
611 // Don't change the group id once it's set.
612 DCHECK(group_
== -1 || group_
== gid
);
616 int View::GetGroup() const {
620 bool View::IsGroupFocusTraversable() const {
624 void View::GetViewsInGroup(int group
, Views
* views
) {
626 views
->push_back(this);
628 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
629 child_at(i
)->GetViewsInGroup(group
, views
);
632 View
* View::GetSelectedViewForGroup(int group
) {
634 GetWidget()->GetRootView()->GetViewsInGroup(group
, &views
);
635 return views
.empty() ? NULL
: views
[0];
638 // Coordinate conversion -------------------------------------------------------
641 void View::ConvertPointToTarget(const View
* source
,
646 if (source
== target
)
649 const View
* root
= GetHierarchyRoot(target
);
650 CHECK_EQ(GetHierarchyRoot(source
), root
);
653 source
->ConvertPointForAncestor(root
, point
);
656 target
->ConvertPointFromAncestor(root
, point
);
660 void View::ConvertRectToTarget(const View
* source
,
665 if (source
== target
)
668 const View
* root
= GetHierarchyRoot(target
);
669 CHECK_EQ(GetHierarchyRoot(source
), root
);
672 source
->ConvertRectForAncestor(root
, rect
);
675 target
->ConvertRectFromAncestor(root
, rect
);
679 void View::ConvertPointToWidget(const View
* src
, gfx::Point
* p
) {
683 src
->ConvertPointForAncestor(NULL
, p
);
687 void View::ConvertPointFromWidget(const View
* dest
, gfx::Point
* p
) {
691 dest
->ConvertPointFromAncestor(NULL
, p
);
695 void View::ConvertPointToScreen(const View
* src
, gfx::Point
* p
) {
699 // If the view is not connected to a tree, there's nothing we can do.
700 const Widget
* widget
= src
->GetWidget();
702 ConvertPointToWidget(src
, p
);
703 *p
+= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
708 void View::ConvertPointFromScreen(const View
* dst
, gfx::Point
* p
) {
712 const views::Widget
* widget
= dst
->GetWidget();
715 *p
-= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
716 views::View::ConvertPointFromWidget(dst
, p
);
719 gfx::Rect
View::ConvertRectToParent(const gfx::Rect
& rect
) const {
720 gfx::RectF x_rect
= rect
;
721 GetTransform().TransformRect(&x_rect
);
722 x_rect
.Offset(GetMirroredPosition().OffsetFromOrigin());
723 // Pixels we partially occupy in the parent should be included.
724 return gfx::ToEnclosingRect(x_rect
);
727 gfx::Rect
View::ConvertRectToWidget(const gfx::Rect
& rect
) const {
728 gfx::Rect x_rect
= rect
;
729 for (const View
* v
= this; v
; v
= v
->parent_
)
730 x_rect
= v
->ConvertRectToParent(x_rect
);
734 // Painting --------------------------------------------------------------------
736 void View::SchedulePaint() {
737 SchedulePaintInRect(GetLocalBounds());
740 void View::SchedulePaintInRect(const gfx::Rect
& rect
) {
745 layer()->SchedulePaint(rect
);
746 } else if (parent_
) {
747 // Translate the requested paint rect to the parent's coordinate system
748 // then pass this notification up to the parent.
749 parent_
->SchedulePaintInRect(ConvertRectToParent(rect
));
753 void View::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
754 // The cull_set may allow us to skip painting without canvas construction or
755 // even canvas rect intersection.
756 if (cull_set
.ShouldPaint(this)) {
757 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
759 gfx::ScopedCanvas
scoped_canvas(canvas
);
761 // Paint this View and its children, setting the clip rect to the bounds
762 // of this View and translating the origin to the local bounds' top left
765 // Note that the X (or left) position we pass to ClipRectInt takes into
766 // consideration whether or not the view uses a right-to-left layout so that
767 // we paint our view in its mirrored position if need be.
768 gfx::Rect clip_rect
= bounds();
769 clip_rect
.Inset(clip_insets_
);
771 clip_rect
.set_x(parent_
->GetMirroredXForRect(clip_rect
));
772 canvas
->ClipRect(clip_rect
);
773 if (canvas
->IsClipEmpty())
776 // Non-empty clip, translate the graphics such that 0,0 corresponds to where
777 // this view is located (related to its parent).
778 canvas
->Translate(GetMirroredPosition().OffsetFromOrigin());
779 canvas
->Transform(GetTransform());
781 // If we are a paint root, we need to construct our own CullSet object for
782 // propagation to our children.
785 bounds_tree_
.reset(new BoundsTree(2, 5));
787 // Recompute our bounds tree as needed.
788 UpdateRootBounds(bounds_tree_
.get(), gfx::Vector2d());
790 // Grab the clip rect from the supplied canvas to use as the query rect.
791 gfx::Rect canvas_bounds
;
792 if (!canvas
->GetClipBounds(&canvas_bounds
)) {
793 NOTREACHED() << "Failed to get clip bounds from the canvas!";
797 // Now query our bounds_tree_ for a set of damaged views that intersect
798 // our canvas bounds.
799 scoped_ptr
<base::hash_set
<intptr_t> > damaged_views(
800 new base::hash_set
<intptr_t>());
801 bounds_tree_
->AppendIntersectingRecords(
802 canvas_bounds
, damaged_views
.get());
803 // Construct a CullSet to wrap the damaged views set, it will delete it
804 // for us on scope exit.
805 CullSet
paint_root_cull_set(damaged_views
.Pass());
806 // Paint all descendents using our new cull set.
807 PaintCommon(canvas
, paint_root_cull_set
);
809 // Not a paint root, so we can proceed as normal.
810 PaintCommon(canvas
, cull_set
);
815 void View::set_background(Background
* b
) {
816 background_
.reset(b
);
819 void View::SetBorder(scoped_ptr
<Border
> b
) { border_
= b
.Pass(); }
821 ui::ThemeProvider
* View::GetThemeProvider() const {
822 const Widget
* widget
= GetWidget();
823 return widget
? widget
->GetThemeProvider() : NULL
;
826 const ui::NativeTheme
* View::GetNativeTheme() const {
827 const Widget
* widget
= GetWidget();
828 return widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
831 // Input -----------------------------------------------------------------------
833 View
* View::GetEventHandlerForPoint(const gfx::Point
& point
) {
834 return GetEventHandlerForRect(gfx::Rect(point
, gfx::Size(1, 1)));
837 View
* View::GetEventHandlerForRect(const gfx::Rect
& rect
) {
838 // |rect_view| represents the current best candidate to return
839 // if rect-based targeting (i.e., fuzzing) is used.
840 // |rect_view_distance| is used to keep track of the distance
841 // between the center point of |rect_view| and the center
843 View
* rect_view
= NULL
;
844 int rect_view_distance
= INT_MAX
;
846 // |point_view| represents the view that would have been returned
847 // from this function call if point-based targeting were used.
848 View
* point_view
= NULL
;
850 for (int i
= child_count() - 1; i
>= 0; --i
) {
851 View
* child
= child_at(i
);
853 if (!child
->CanProcessEventsWithinSubtree())
856 // Ignore any children which are invisible or do not intersect |rect|.
857 if (!child
->visible())
859 gfx::RectF
rect_in_child_coords_f(rect
);
860 ConvertRectToTarget(this, child
, &rect_in_child_coords_f
);
861 gfx::Rect rect_in_child_coords
= gfx::ToEnclosingRect(
862 rect_in_child_coords_f
);
863 if (!child
->HitTestRect(rect_in_child_coords
))
866 View
* cur_view
= child
->GetEventHandlerForRect(rect_in_child_coords
);
868 if (views::UsePointBasedTargeting(rect
))
871 gfx::RectF
cur_view_bounds_f(cur_view
->GetLocalBounds());
872 ConvertRectToTarget(cur_view
, this, &cur_view_bounds_f
);
873 gfx::Rect cur_view_bounds
= gfx::ToEnclosingRect(
875 if (views::PercentCoveredBy(cur_view_bounds
, rect
) >= kRectTargetOverlap
) {
876 // |cur_view| is a suitable candidate for rect-based targeting.
877 // Check to see if it is the closest suitable candidate so far.
878 gfx::Point
touch_center(rect
.CenterPoint());
879 int cur_dist
= views::DistanceSquaredFromCenterToPoint(touch_center
,
881 if (!rect_view
|| cur_dist
< rect_view_distance
) {
882 rect_view
= cur_view
;
883 rect_view_distance
= cur_dist
;
885 } else if (!rect_view
&& !point_view
) {
886 // Rect-based targeting has not yielded any candidates so far. Check
887 // if point-based targeting would have selected |cur_view|.
888 gfx::Point
point_in_child_coords(rect_in_child_coords
.CenterPoint());
889 if (child
->HitTestPoint(point_in_child_coords
))
890 point_view
= child
->GetEventHandlerForPoint(point_in_child_coords
);
894 if (views::UsePointBasedTargeting(rect
) || (!rect_view
&& !point_view
))
897 // If |this| is a suitable candidate for rect-based targeting, check to
898 // see if it is closer than the current best suitable candidate so far.
899 gfx::Rect
local_bounds(GetLocalBounds());
900 if (views::PercentCoveredBy(local_bounds
, rect
) >= kRectTargetOverlap
) {
901 gfx::Point
touch_center(rect
.CenterPoint());
902 int cur_dist
= views::DistanceSquaredFromCenterToPoint(touch_center
,
904 if (!rect_view
|| cur_dist
< rect_view_distance
)
908 return rect_view
? rect_view
: point_view
;
911 bool View::CanProcessEventsWithinSubtree() const {
915 View
* View::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
916 if (!HitTestPoint(point
) || !CanProcessEventsWithinSubtree())
919 // Walk the child Views recursively looking for the View that most
920 // tightly encloses the specified point.
921 for (int i
= child_count() - 1; i
>= 0; --i
) {
922 View
* child
= child_at(i
);
923 if (!child
->visible())
926 gfx::Point
point_in_child_coords(point
);
927 ConvertPointToTarget(this, child
, &point_in_child_coords
);
928 View
* handler
= child
->GetTooltipHandlerForPoint(point_in_child_coords
);
935 gfx::NativeCursor
View::GetCursor(const ui::MouseEvent
& event
) {
937 static ui::Cursor arrow
;
938 if (!arrow
.platform())
939 arrow
.SetPlatformCursor(LoadCursor(NULL
, IDC_ARROW
));
942 return gfx::kNullCursor
;
946 bool View::HitTestPoint(const gfx::Point
& point
) const {
947 return HitTestRect(gfx::Rect(point
, gfx::Size(1, 1)));
950 bool View::HitTestRect(const gfx::Rect
& rect
) const {
951 if (GetLocalBounds().Intersects(rect
)) {
952 if (HasHitTestMask()) {
954 HitTestSource source
= HIT_TEST_SOURCE_MOUSE
;
955 if (!views::UsePointBasedTargeting(rect
))
956 source
= HIT_TEST_SOURCE_TOUCH
;
957 GetHitTestMask(source
, &mask
);
958 SkRegion clip_region
;
959 clip_region
.setRect(0, 0, width(), height());
960 SkRegion mask_region
;
961 return mask_region
.setPath(mask
, clip_region
) &&
962 mask_region
.intersects(RectToSkIRect(rect
));
964 // No mask, but inside our bounds.
967 // Outside our bounds.
971 bool View::IsMouseHovered() {
972 // If we haven't yet been placed in an onscreen view hierarchy, we can't be
977 // If mouse events are disabled, then the mouse cursor is invisible and
978 // is therefore not hovering over this button.
979 if (!GetWidget()->IsMouseEventsEnabled())
982 gfx::Point
cursor_pos(gfx::Screen::GetScreenFor(
983 GetWidget()->GetNativeView())->GetCursorScreenPoint());
984 ConvertPointFromScreen(this, &cursor_pos
);
985 return HitTestPoint(cursor_pos
);
988 bool View::OnMousePressed(const ui::MouseEvent
& event
) {
992 bool View::OnMouseDragged(const ui::MouseEvent
& event
) {
996 void View::OnMouseReleased(const ui::MouseEvent
& event
) {
999 void View::OnMouseCaptureLost() {
1002 void View::OnMouseMoved(const ui::MouseEvent
& event
) {
1005 void View::OnMouseEntered(const ui::MouseEvent
& event
) {
1008 void View::OnMouseExited(const ui::MouseEvent
& event
) {
1011 void View::SetMouseHandler(View
* new_mouse_handler
) {
1012 // |new_mouse_handler| may be NULL.
1014 parent_
->SetMouseHandler(new_mouse_handler
);
1017 bool View::OnKeyPressed(const ui::KeyEvent
& event
) {
1021 bool View::OnKeyReleased(const ui::KeyEvent
& event
) {
1025 bool View::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
1029 void View::OnKeyEvent(ui::KeyEvent
* event
) {
1030 bool consumed
= (event
->type() == ui::ET_KEY_PRESSED
) ? OnKeyPressed(*event
) :
1031 OnKeyReleased(*event
);
1033 event
->StopPropagation();
1036 void View::OnMouseEvent(ui::MouseEvent
* event
) {
1037 switch (event
->type()) {
1038 case ui::ET_MOUSE_PRESSED
:
1039 if (ProcessMousePressed(*event
))
1040 event
->SetHandled();
1043 case ui::ET_MOUSE_MOVED
:
1044 if ((event
->flags() & (ui::EF_LEFT_MOUSE_BUTTON
|
1045 ui::EF_RIGHT_MOUSE_BUTTON
|
1046 ui::EF_MIDDLE_MOUSE_BUTTON
)) == 0) {
1047 OnMouseMoved(*event
);
1051 case ui::ET_MOUSE_DRAGGED
:
1052 if (ProcessMouseDragged(*event
))
1053 event
->SetHandled();
1056 case ui::ET_MOUSE_RELEASED
:
1057 ProcessMouseReleased(*event
);
1060 case ui::ET_MOUSEWHEEL
:
1061 if (OnMouseWheel(*static_cast<ui::MouseWheelEvent
*>(event
)))
1062 event
->SetHandled();
1065 case ui::ET_MOUSE_ENTERED
:
1066 if (event
->flags() & ui::EF_TOUCH_ACCESSIBILITY
)
1067 NotifyAccessibilityEvent(ui::AX_EVENT_HOVER
, true);
1068 OnMouseEntered(*event
);
1071 case ui::ET_MOUSE_EXITED
:
1072 OnMouseExited(*event
);
1080 void View::OnScrollEvent(ui::ScrollEvent
* event
) {
1083 void View::OnTouchEvent(ui::TouchEvent
* event
) {
1084 NOTREACHED() << "Views should not receive touch events.";
1087 void View::OnGestureEvent(ui::GestureEvent
* event
) {
1090 ui::TextInputClient
* View::GetTextInputClient() {
1094 InputMethod
* View::GetInputMethod() {
1095 Widget
* widget
= GetWidget();
1096 return widget
? widget
->GetInputMethod() : NULL
;
1099 const InputMethod
* View::GetInputMethod() const {
1100 const Widget
* widget
= GetWidget();
1101 return widget
? widget
->GetInputMethod() : NULL
;
1104 scoped_ptr
<ui::EventTargeter
>
1105 View::SetEventTargeter(scoped_ptr
<ui::EventTargeter
> targeter
) {
1106 scoped_ptr
<ui::EventTargeter
> old_targeter
= targeter_
.Pass();
1107 targeter_
= targeter
.Pass();
1108 return old_targeter
.Pass();
1111 bool View::CanAcceptEvent(const ui::Event
& event
) {
1115 ui::EventTarget
* View::GetParentTarget() {
1119 scoped_ptr
<ui::EventTargetIterator
> View::GetChildIterator() const {
1120 return scoped_ptr
<ui::EventTargetIterator
>(
1121 new ui::EventTargetIteratorImpl
<View
>(children_
));
1124 ui::EventTargeter
* View::GetEventTargeter() {
1125 return targeter_
.get();
1128 void View::ConvertEventToTarget(ui::EventTarget
* target
,
1129 ui::LocatedEvent
* event
) {
1130 event
->ConvertLocationToTarget(this, static_cast<View
*>(target
));
1133 // Accelerators ----------------------------------------------------------------
1135 void View::AddAccelerator(const ui::Accelerator
& accelerator
) {
1136 if (!accelerators_
.get())
1137 accelerators_
.reset(new std::vector
<ui::Accelerator
>());
1139 if (std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
) ==
1140 accelerators_
->end()) {
1141 accelerators_
->push_back(accelerator
);
1143 RegisterPendingAccelerators();
1146 void View::RemoveAccelerator(const ui::Accelerator
& accelerator
) {
1147 if (!accelerators_
.get()) {
1148 NOTREACHED() << "Removing non-existing accelerator";
1152 std::vector
<ui::Accelerator
>::iterator
i(
1153 std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
));
1154 if (i
== accelerators_
->end()) {
1155 NOTREACHED() << "Removing non-existing accelerator";
1159 size_t index
= i
- accelerators_
->begin();
1160 accelerators_
->erase(i
);
1161 if (index
>= registered_accelerator_count_
) {
1162 // The accelerator is not registered to FocusManager.
1165 --registered_accelerator_count_
;
1167 // Providing we are attached to a Widget and registered with a focus manager,
1168 // we should de-register from that focus manager now.
1169 if (GetWidget() && accelerator_focus_manager_
)
1170 accelerator_focus_manager_
->UnregisterAccelerator(accelerator
, this);
1173 void View::ResetAccelerators() {
1174 if (accelerators_
.get())
1175 UnregisterAccelerators(false);
1178 bool View::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1182 bool View::CanHandleAccelerators() const {
1183 return enabled() && IsDrawn() && GetWidget() && GetWidget()->IsVisible();
1186 // Focus -----------------------------------------------------------------------
1188 bool View::HasFocus() const {
1189 const FocusManager
* focus_manager
= GetFocusManager();
1190 return focus_manager
&& (focus_manager
->GetFocusedView() == this);
1193 View
* View::GetNextFocusableView() {
1194 return next_focusable_view_
;
1197 const View
* View::GetNextFocusableView() const {
1198 return next_focusable_view_
;
1201 View
* View::GetPreviousFocusableView() {
1202 return previous_focusable_view_
;
1205 void View::SetNextFocusableView(View
* view
) {
1207 view
->previous_focusable_view_
= this;
1208 next_focusable_view_
= view
;
1211 void View::SetFocusable(bool focusable
) {
1212 if (focusable_
== focusable
)
1215 focusable_
= focusable
;
1218 bool View::IsFocusable() const {
1219 return focusable_
&& enabled_
&& IsDrawn();
1222 bool View::IsAccessibilityFocusable() const {
1223 return (focusable_
|| accessibility_focusable_
) && enabled_
&& IsDrawn();
1226 void View::SetAccessibilityFocusable(bool accessibility_focusable
) {
1227 if (accessibility_focusable_
== accessibility_focusable
)
1230 accessibility_focusable_
= accessibility_focusable
;
1233 FocusManager
* View::GetFocusManager() {
1234 Widget
* widget
= GetWidget();
1235 return widget
? widget
->GetFocusManager() : NULL
;
1238 const FocusManager
* View::GetFocusManager() const {
1239 const Widget
* widget
= GetWidget();
1240 return widget
? widget
->GetFocusManager() : NULL
;
1243 void View::RequestFocus() {
1244 FocusManager
* focus_manager
= GetFocusManager();
1245 if (focus_manager
&& IsFocusable())
1246 focus_manager
->SetFocusedView(this);
1249 bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) {
1253 FocusTraversable
* View::GetFocusTraversable() {
1257 FocusTraversable
* View::GetPaneFocusTraversable() {
1261 // Tooltips --------------------------------------------------------------------
1263 bool View::GetTooltipText(const gfx::Point
& p
, base::string16
* tooltip
) const {
1267 bool View::GetTooltipTextOrigin(const gfx::Point
& p
, gfx::Point
* loc
) const {
1271 // Context menus ---------------------------------------------------------------
1273 void View::ShowContextMenu(const gfx::Point
& p
,
1274 ui::MenuSourceType source_type
) {
1275 if (!context_menu_controller_
)
1278 context_menu_controller_
->ShowContextMenuForView(this, p
, source_type
);
1282 bool View::ShouldShowContextMenuOnMousePress() {
1283 return kContextMenuOnMousePress
;
1286 // Drag and drop ---------------------------------------------------------------
1288 bool View::GetDropFormats(
1290 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
1294 bool View::AreDropTypesRequired() {
1298 bool View::CanDrop(const OSExchangeData
& data
) {
1299 // TODO(sky): when I finish up migration, this should default to true.
1303 void View::OnDragEntered(const ui::DropTargetEvent
& event
) {
1306 int View::OnDragUpdated(const ui::DropTargetEvent
& event
) {
1307 return ui::DragDropTypes::DRAG_NONE
;
1310 void View::OnDragExited() {
1313 int View::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1314 return ui::DragDropTypes::DRAG_NONE
;
1317 void View::OnDragDone() {
1321 bool View::ExceededDragThreshold(const gfx::Vector2d
& delta
) {
1322 return (abs(delta
.x()) > GetHorizontalDragThreshold() ||
1323 abs(delta
.y()) > GetVerticalDragThreshold());
1326 // Accessibility----------------------------------------------------------------
1328 gfx::NativeViewAccessible
View::GetNativeViewAccessible() {
1329 if (!native_view_accessibility_
)
1330 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1331 if (native_view_accessibility_
)
1332 return native_view_accessibility_
->GetNativeObject();
1336 void View::NotifyAccessibilityEvent(
1337 ui::AXEvent event_type
,
1338 bool send_native_event
) {
1339 if (ViewsDelegate::views_delegate
)
1340 ViewsDelegate::views_delegate
->NotifyAccessibilityEvent(this, event_type
);
1342 if (send_native_event
&& GetWidget()) {
1343 if (!native_view_accessibility_
)
1344 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1345 if (native_view_accessibility_
)
1346 native_view_accessibility_
->NotifyAccessibilityEvent(event_type
);
1350 // Scrolling -------------------------------------------------------------------
1352 void View::ScrollRectToVisible(const gfx::Rect
& rect
) {
1353 // We must take RTL UI mirroring into account when adjusting the position of
1356 gfx::Rect
scroll_rect(rect
);
1357 scroll_rect
.Offset(GetMirroredX(), y());
1358 parent_
->ScrollRectToVisible(scroll_rect
);
1362 int View::GetPageScrollIncrement(ScrollView
* scroll_view
,
1363 bool is_horizontal
, bool is_positive
) {
1367 int View::GetLineScrollIncrement(ScrollView
* scroll_view
,
1368 bool is_horizontal
, bool is_positive
) {
1372 ////////////////////////////////////////////////////////////////////////////////
1375 // Size and disposition --------------------------------------------------------
1377 void View::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1380 void View::PreferredSizeChanged() {
1383 parent_
->ChildPreferredSizeChanged(this);
1386 bool View::NeedsNotificationWhenVisibleBoundsChange() const {
1390 void View::OnVisibleBoundsChanged() {
1393 // Tree operations -------------------------------------------------------------
1395 void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails
& details
) {
1398 void View::VisibilityChanged(View
* starting_from
, bool is_visible
) {
1401 void View::NativeViewHierarchyChanged() {
1402 FocusManager
* focus_manager
= GetFocusManager();
1403 if (accelerator_focus_manager_
!= focus_manager
) {
1404 UnregisterAccelerators(true);
1407 RegisterPendingAccelerators();
1411 // Painting --------------------------------------------------------------------
1413 void View::PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1414 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1415 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1416 if (!child_at(i
)->layer())
1417 child_at(i
)->Paint(canvas
, cull_set
);
1420 void View::OnPaint(gfx::Canvas
* canvas
) {
1421 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1422 OnPaintBackground(canvas
);
1423 OnPaintBorder(canvas
);
1426 void View::OnPaintBackground(gfx::Canvas
* canvas
) {
1427 if (background_
.get()) {
1428 TRACE_EVENT2("views", "View::OnPaintBackground",
1429 "width", canvas
->sk_canvas()->getDevice()->width(),
1430 "height", canvas
->sk_canvas()->getDevice()->height());
1431 background_
->Paint(canvas
, this);
1435 void View::OnPaintBorder(gfx::Canvas
* canvas
) {
1436 if (border_
.get()) {
1437 TRACE_EVENT2("views", "View::OnPaintBorder",
1438 "width", canvas
->sk_canvas()->getDevice()->width(),
1439 "height", canvas
->sk_canvas()->getDevice()->height());
1440 border_
->Paint(*this, canvas
);
1444 bool View::IsPaintRoot() {
1445 return paint_to_layer_
|| !parent_
;
1448 // Accelerated Painting --------------------------------------------------------
1450 void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely
) {
1451 // This method should not have the side-effect of creating the layer.
1453 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely
);
1456 gfx::Vector2d
View::CalculateOffsetToAncestorWithLayer(
1457 ui::Layer
** layer_parent
) {
1460 *layer_parent
= layer();
1461 return gfx::Vector2d();
1464 return gfx::Vector2d();
1466 return gfx::Vector2d(GetMirroredX(), y()) +
1467 parent_
->CalculateOffsetToAncestorWithLayer(layer_parent
);
1470 void View::UpdateParentLayer() {
1474 ui::Layer
* parent_layer
= NULL
;
1475 gfx::Vector2d
offset(GetMirroredX(), y());
1478 offset
+= parent_
->CalculateOffsetToAncestorWithLayer(&parent_layer
);
1480 ReparentLayer(offset
, parent_layer
);
1483 void View::MoveLayerToParent(ui::Layer
* parent_layer
,
1484 const gfx::Point
& point
) {
1485 gfx::Point
local_point(point
);
1486 if (parent_layer
!= layer())
1487 local_point
.Offset(GetMirroredX(), y());
1488 if (layer() && parent_layer
!= layer()) {
1489 parent_layer
->Add(layer());
1490 SetLayerBounds(gfx::Rect(local_point
.x(), local_point
.y(),
1491 width(), height()));
1493 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1494 child_at(i
)->MoveLayerToParent(parent_layer
, local_point
);
1498 void View::UpdateLayerVisibility() {
1499 bool visible
= visible_
;
1500 for (const View
* v
= parent_
; visible
&& v
&& !v
->layer(); v
= v
->parent_
)
1501 visible
= v
->visible();
1503 UpdateChildLayerVisibility(visible
);
1506 void View::UpdateChildLayerVisibility(bool ancestor_visible
) {
1508 layer()->SetVisible(ancestor_visible
&& visible_
);
1510 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1511 child_at(i
)->UpdateChildLayerVisibility(ancestor_visible
&& visible_
);
1515 void View::UpdateChildLayerBounds(const gfx::Vector2d
& offset
) {
1517 SetLayerBounds(GetLocalBounds() + offset
);
1519 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
1520 View
* child
= child_at(i
);
1521 child
->UpdateChildLayerBounds(
1522 offset
+ gfx::Vector2d(child
->GetMirroredX(), child
->y()));
1527 void View::OnPaintLayer(gfx::Canvas
* canvas
) {
1528 if (!layer() || !layer()->fills_bounds_opaquely())
1529 canvas
->DrawColor(SK_ColorBLACK
, SkXfermode::kClear_Mode
);
1530 PaintCommon(canvas
, CullSet());
1533 void View::OnDeviceScaleFactorChanged(float device_scale_factor
) {
1534 // Repainting with new scale factor will paint the content at the right scale.
1537 base::Closure
View::PrepareForLayerBoundsChange() {
1538 return base::Closure();
1541 void View::ReorderLayers() {
1543 while (v
&& !v
->layer())
1546 Widget
* widget
= GetWidget();
1549 ui::Layer
* layer
= widget
->GetLayer();
1551 widget
->GetRootView()->ReorderChildLayers(layer
);
1554 v
->ReorderChildLayers(v
->layer());
1558 // Reorder the widget's child NativeViews in case a child NativeView is
1559 // associated with a view (eg via a NativeViewHost). Always do the
1560 // reordering because the associated NativeView's layer (if it has one)
1561 // is parented to the widget's layer regardless of whether the host view has
1562 // an ancestor with a layer.
1563 widget
->ReorderNativeViews();
1567 void View::ReorderChildLayers(ui::Layer
* parent_layer
) {
1568 if (layer() && layer() != parent_layer
) {
1569 DCHECK_EQ(parent_layer
, layer()->parent());
1570 parent_layer
->StackAtBottom(layer());
1572 // Iterate backwards through the children so that a child with a layer
1573 // which is further to the back is stacked above one which is further to
1575 for (Views::reverse_iterator
it(children_
.rbegin());
1576 it
!= children_
.rend(); ++it
) {
1577 (*it
)->ReorderChildLayers(parent_layer
);
1582 // Input -----------------------------------------------------------------------
1584 bool View::HasHitTestMask() const {
1588 void View::GetHitTestMask(HitTestSource source
, gfx::Path
* mask
) const {
1592 View::DragInfo
* View::GetDragInfo() {
1593 return parent_
? parent_
->GetDragInfo() : NULL
;
1596 // Focus -----------------------------------------------------------------------
1598 void View::OnFocus() {
1599 // TODO(beng): Investigate whether it's possible for us to move this to
1601 // By default, we clear the native focus. This ensures that no visible native
1602 // view as the focus and that we still receive keyboard inputs.
1603 FocusManager
* focus_manager
= GetFocusManager();
1605 focus_manager
->ClearNativeFocus();
1607 // TODO(beng): Investigate whether it's possible for us to move this to
1609 // Notify assistive technologies of the focus change.
1610 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
1613 void View::OnBlur() {
1616 void View::Focus() {
1624 // Tooltips --------------------------------------------------------------------
1626 void View::TooltipTextChanged() {
1627 Widget
* widget
= GetWidget();
1628 // TooltipManager may be null if there is a problem creating it.
1629 if (widget
&& widget
->GetTooltipManager())
1630 widget
->GetTooltipManager()->TooltipTextChanged(this);
1633 // Context menus ---------------------------------------------------------------
1635 gfx::Point
View::GetKeyboardContextMenuLocation() {
1636 gfx::Rect vis_bounds
= GetVisibleBounds();
1637 gfx::Point
screen_point(vis_bounds
.x() + vis_bounds
.width() / 2,
1638 vis_bounds
.y() + vis_bounds
.height() / 2);
1639 ConvertPointToScreen(this, &screen_point
);
1640 return screen_point
;
1643 // Drag and drop ---------------------------------------------------------------
1645 int View::GetDragOperations(const gfx::Point
& press_pt
) {
1646 return drag_controller_
?
1647 drag_controller_
->GetDragOperationsForView(this, press_pt
) :
1648 ui::DragDropTypes::DRAG_NONE
;
1651 void View::WriteDragData(const gfx::Point
& press_pt
, OSExchangeData
* data
) {
1652 DCHECK(drag_controller_
);
1653 drag_controller_
->WriteDragDataForView(this, press_pt
, data
);
1656 bool View::InDrag() {
1657 Widget
* widget
= GetWidget();
1658 return widget
? widget
->dragged_view() == this : false;
1661 int View::GetHorizontalDragThreshold() {
1662 // TODO(jennyz): This value may need to be adjusted for different platforms
1663 // and for different display density.
1664 return kDefaultHorizontalDragThreshold
;
1667 int View::GetVerticalDragThreshold() {
1668 // TODO(jennyz): This value may need to be adjusted for different platforms
1669 // and for different display density.
1670 return kDefaultVerticalDragThreshold
;
1673 // Debugging -------------------------------------------------------------------
1675 #if !defined(NDEBUG)
1677 std::string
View::PrintViewGraph(bool first
) {
1678 return DoPrintViewGraph(first
, this);
1681 std::string
View::DoPrintViewGraph(bool first
, View
* view_with_children
) {
1682 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1683 const size_t kMaxPointerStringLength
= 19;
1688 result
.append("digraph {\n");
1690 // Node characteristics.
1691 char p
[kMaxPointerStringLength
];
1693 const std::string
class_name(GetClassName());
1694 size_t base_name_index
= class_name
.find_last_of('/');
1695 if (base_name_index
== std::string::npos
)
1696 base_name_index
= 0;
1700 char bounds_buffer
[512];
1702 // Information about current node.
1703 base::snprintf(p
, arraysize(bounds_buffer
), "%p", view_with_children
);
1704 result
.append(" N");
1705 result
.append(p
+ 2);
1706 result
.append(" [label=\"");
1708 result
.append(class_name
.substr(base_name_index
).c_str());
1710 base::snprintf(bounds_buffer
,
1711 arraysize(bounds_buffer
),
1712 "\\n bounds: (%d, %d), (%dx%d)",
1717 result
.append(bounds_buffer
);
1719 gfx::DecomposedTransform decomp
;
1720 if (!GetTransform().IsIdentity() &&
1721 gfx::DecomposeTransform(&decomp
, GetTransform())) {
1722 base::snprintf(bounds_buffer
,
1723 arraysize(bounds_buffer
),
1724 "\\n translation: (%f, %f)",
1725 decomp
.translate
[0],
1726 decomp
.translate
[1]);
1727 result
.append(bounds_buffer
);
1729 base::snprintf(bounds_buffer
,
1730 arraysize(bounds_buffer
),
1731 "\\n rotation: %3.2f",
1732 std::acos(decomp
.quaternion
[3]) * 360.0 / M_PI
);
1733 result
.append(bounds_buffer
);
1735 base::snprintf(bounds_buffer
,
1736 arraysize(bounds_buffer
),
1737 "\\n scale: (%2.4f, %2.4f)",
1740 result
.append(bounds_buffer
);
1743 result
.append("\"");
1745 result
.append(", shape=box");
1747 if (layer()->has_external_content())
1748 result
.append(", color=green");
1750 result
.append(", color=red");
1752 if (layer()->fills_bounds_opaquely())
1753 result
.append(", style=filled");
1755 result
.append("]\n");
1759 char pp
[kMaxPointerStringLength
];
1761 base::snprintf(pp
, kMaxPointerStringLength
, "%p", parent_
);
1762 result
.append(" N");
1763 result
.append(pp
+ 2);
1764 result
.append(" -> N");
1765 result
.append(p
+ 2);
1766 result
.append("\n");
1770 for (int i
= 0, count
= view_with_children
->child_count(); i
< count
; ++i
)
1771 result
.append(view_with_children
->child_at(i
)->PrintViewGraph(false));
1774 result
.append("}\n");
1780 ////////////////////////////////////////////////////////////////////////////////
1783 // DropInfo --------------------------------------------------------------------
1785 void View::DragInfo::Reset() {
1786 possible_drag
= false;
1787 start_pt
= gfx::Point();
1790 void View::DragInfo::PossibleDrag(const gfx::Point
& p
) {
1791 possible_drag
= true;
1795 // Painting --------------------------------------------------------------------
1797 void View::SchedulePaintBoundsChanged(SchedulePaintType type
) {
1798 // If we have a layer and the View's size did not change, we do not need to
1799 // schedule any paints since the layer will be redrawn at its new location
1800 // during the next Draw() cycle in the compositor.
1801 if (!layer() || type
== SCHEDULE_PAINT_SIZE_CHANGED
) {
1802 // Otherwise, if the size changes or we don't have a layer then we need to
1803 // use SchedulePaint to invalidate the area occupied by the View.
1805 } else if (parent_
&& type
== SCHEDULE_PAINT_SIZE_SAME
) {
1806 // The compositor doesn't Draw() until something on screen changes, so
1807 // if our position changes but nothing is being animated on screen, then
1808 // tell the compositor to redraw the scene. We know layer() exists due to
1809 // the above if clause.
1810 layer()->ScheduleDraw();
1814 void View::PaintCommon(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1819 // If the View we are about to paint requested the canvas to be flipped, we
1820 // should change the transform appropriately.
1821 // The canvas mirroring is undone once the View is done painting so that we
1822 // don't pass the canvas with the mirrored transform to Views that didn't
1823 // request the canvas to be flipped.
1824 gfx::ScopedCanvas
scoped(canvas
);
1825 if (FlipCanvasOnPaintForRTLUI()) {
1826 canvas
->Translate(gfx::Vector2d(width(), 0));
1827 canvas
->Scale(-1, 1);
1833 PaintChildren(canvas
, cull_set
);
1836 // Tree operations -------------------------------------------------------------
1838 void View::DoRemoveChildView(View
* view
,
1839 bool update_focus_cycle
,
1840 bool update_tool_tip
,
1841 bool delete_removed_view
,
1845 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
1846 scoped_ptr
<View
> view_to_be_deleted
;
1847 if (i
!= children_
.end()) {
1848 if (update_focus_cycle
) {
1849 // Let's remove the view from the focus traversal.
1850 View
* next_focusable
= view
->next_focusable_view_
;
1851 View
* prev_focusable
= view
->previous_focusable_view_
;
1853 prev_focusable
->next_focusable_view_
= next_focusable
;
1855 next_focusable
->previous_focusable_view_
= prev_focusable
;
1859 UnregisterChildrenForVisibleBoundsNotification(view
);
1860 if (view
->visible())
1861 view
->SchedulePaint();
1862 GetWidget()->NotifyWillRemoveView(view
);
1865 // Remove the bounds of this child and any of its descendants from our
1866 // paint root bounds tree.
1867 BoundsTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
1869 view
->RemoveRootBounds(bounds_tree
);
1871 view
->PropagateRemoveNotifications(this, new_parent
);
1872 view
->parent_
= NULL
;
1873 view
->UpdateLayerVisibility();
1875 if (delete_removed_view
&& !view
->owned_by_client_
)
1876 view_to_be_deleted
.reset(view
);
1881 if (update_tool_tip
)
1884 if (layout_manager_
.get())
1885 layout_manager_
->ViewRemoved(this, view
);
1888 void View::PropagateRemoveNotifications(View
* old_parent
, View
* new_parent
) {
1889 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1890 child_at(i
)->PropagateRemoveNotifications(old_parent
, new_parent
);
1892 ViewHierarchyChangedDetails
details(false, old_parent
, this, new_parent
);
1893 for (View
* v
= this; v
; v
= v
->parent_
)
1894 v
->ViewHierarchyChangedImpl(true, details
);
1897 void View::PropagateAddNotifications(
1898 const ViewHierarchyChangedDetails
& details
) {
1899 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1900 child_at(i
)->PropagateAddNotifications(details
);
1901 ViewHierarchyChangedImpl(true, details
);
1904 void View::PropagateNativeViewHierarchyChanged() {
1905 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1906 child_at(i
)->PropagateNativeViewHierarchyChanged();
1907 NativeViewHierarchyChanged();
1910 void View::ViewHierarchyChangedImpl(
1911 bool register_accelerators
,
1912 const ViewHierarchyChangedDetails
& details
) {
1913 if (register_accelerators
) {
1914 if (details
.is_add
) {
1915 // If you get this registration, you are part of a subtree that has been
1916 // added to the view hierarchy.
1917 if (GetFocusManager())
1918 RegisterPendingAccelerators();
1920 if (details
.child
== this)
1921 UnregisterAccelerators(true);
1925 if (details
.is_add
&& layer() && !layer()->parent()) {
1926 UpdateParentLayer();
1927 Widget
* widget
= GetWidget();
1929 widget
->UpdateRootLayers();
1930 } else if (!details
.is_add
&& details
.child
== this) {
1931 // Make sure the layers belonging to the subtree rooted at |child| get
1932 // removed from layers that do not belong in the same subtree.
1934 Widget
* widget
= GetWidget();
1936 widget
->UpdateRootLayers();
1939 ViewHierarchyChanged(details
);
1940 details
.parent
->needs_layout_
= true;
1943 void View::PropagateNativeThemeChanged(const ui::NativeTheme
* theme
) {
1944 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1945 child_at(i
)->PropagateNativeThemeChanged(theme
);
1946 OnNativeThemeChanged(theme
);
1949 // Size and disposition --------------------------------------------------------
1951 void View::PropagateVisibilityNotifications(View
* start
, bool is_visible
) {
1952 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1953 child_at(i
)->PropagateVisibilityNotifications(start
, is_visible
);
1954 VisibilityChangedImpl(start
, is_visible
);
1957 void View::VisibilityChangedImpl(View
* starting_from
, bool is_visible
) {
1958 VisibilityChanged(starting_from
, is_visible
);
1961 void View::BoundsChanged(const gfx::Rect
& previous_bounds
) {
1962 // Mark our bounds as dirty for the paint root, also see if we need to
1963 // recompute our children's bounds due to origin change.
1964 bool origin_changed
=
1965 previous_bounds
.OffsetFromOrigin() != bounds_
.OffsetFromOrigin();
1966 SetRootBoundsDirty(origin_changed
);
1969 // Paint the new bounds.
1970 SchedulePaintBoundsChanged(
1971 bounds_
.size() == previous_bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
1972 SCHEDULE_PAINT_SIZE_CHANGED
);
1977 SetLayerBounds(GetLocalBounds() +
1978 gfx::Vector2d(GetMirroredX(), y()) +
1979 parent_
->CalculateOffsetToAncestorWithLayer(NULL
));
1981 SetLayerBounds(bounds_
);
1984 // If our bounds have changed, then any descendant layer bounds may have
1985 // changed. Update them accordingly.
1986 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
1989 OnBoundsChanged(previous_bounds
);
1991 if (previous_bounds
.size() != size()) {
1992 needs_layout_
= false;
1996 if (NeedsNotificationWhenVisibleBoundsChange())
1997 OnVisibleBoundsChanged();
1999 // Notify interested Views that visible bounds within the root view may have
2001 if (descendants_to_notify_
.get()) {
2002 for (Views::iterator
i(descendants_to_notify_
->begin());
2003 i
!= descendants_to_notify_
->end(); ++i
) {
2004 (*i
)->OnVisibleBoundsChanged();
2010 void View::RegisterChildrenForVisibleBoundsNotification(View
* view
) {
2011 if (view
->NeedsNotificationWhenVisibleBoundsChange())
2012 view
->RegisterForVisibleBoundsNotification();
2013 for (int i
= 0; i
< view
->child_count(); ++i
)
2014 RegisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
2018 void View::UnregisterChildrenForVisibleBoundsNotification(View
* view
) {
2019 if (view
->NeedsNotificationWhenVisibleBoundsChange())
2020 view
->UnregisterForVisibleBoundsNotification();
2021 for (int i
= 0; i
< view
->child_count(); ++i
)
2022 UnregisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
2025 void View::RegisterForVisibleBoundsNotification() {
2026 if (registered_for_visible_bounds_notification_
)
2029 registered_for_visible_bounds_notification_
= true;
2030 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
2031 ancestor
->AddDescendantToNotify(this);
2034 void View::UnregisterForVisibleBoundsNotification() {
2035 if (!registered_for_visible_bounds_notification_
)
2038 registered_for_visible_bounds_notification_
= false;
2039 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
2040 ancestor
->RemoveDescendantToNotify(this);
2043 void View::AddDescendantToNotify(View
* view
) {
2045 if (!descendants_to_notify_
.get())
2046 descendants_to_notify_
.reset(new Views
);
2047 descendants_to_notify_
->push_back(view
);
2050 void View::RemoveDescendantToNotify(View
* view
) {
2051 DCHECK(view
&& descendants_to_notify_
.get());
2052 Views::iterator
i(std::find(
2053 descendants_to_notify_
->begin(), descendants_to_notify_
->end(), view
));
2054 DCHECK(i
!= descendants_to_notify_
->end());
2055 descendants_to_notify_
->erase(i
);
2056 if (descendants_to_notify_
->empty())
2057 descendants_to_notify_
.reset();
2060 void View::SetLayerBounds(const gfx::Rect
& bounds
) {
2061 layer()->SetBounds(bounds
);
2064 void View::SetRootBoundsDirty(bool origin_changed
) {
2065 root_bounds_dirty_
= true;
2067 if (origin_changed
) {
2068 // Inform our children that their root bounds are dirty, as their relative
2069 // coordinates in paint root space have changed since ours have changed.
2070 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end();
2072 if (!(*i
)->IsPaintRoot())
2073 (*i
)->SetRootBoundsDirty(origin_changed
);
2078 void View::UpdateRootBounds(BoundsTree
* tree
, const gfx::Vector2d
& offset
) {
2079 // No need to recompute bounds if we haven't flagged ours as dirty.
2080 TRACE_EVENT1("views", "View::UpdateRootBounds", "class", GetClassName());
2082 // Add our own offset to the provided offset, for our own bounds update and
2083 // for propagation to our children if needed.
2084 gfx::Vector2d view_offset
= offset
+ GetMirroredBounds().OffsetFromOrigin();
2086 // If our bounds have changed we must re-insert our new bounds to the tree.
2087 if (root_bounds_dirty_
) {
2088 root_bounds_dirty_
= false;
2090 view_offset
.x(), view_offset
.y(), bounds_
.width(), bounds_
.height());
2091 tree
->Insert(bounds
, reinterpret_cast<intptr_t>(this));
2094 // Update our children's bounds if needed.
2095 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2096 // We don't descend in to layer views for bounds recomputation, as they
2097 // manage their own RTree as paint roots.
2098 if (!(*i
)->IsPaintRoot())
2099 (*i
)->UpdateRootBounds(tree
, view_offset
);
2103 void View::RemoveRootBounds(BoundsTree
* tree
) {
2104 tree
->Remove(reinterpret_cast<intptr_t>(this));
2106 root_bounds_dirty_
= true;
2108 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2109 if (!(*i
)->IsPaintRoot())
2110 (*i
)->RemoveRootBounds(tree
);
2114 View::BoundsTree
* View::GetBoundsTreeFromPaintRoot() {
2115 BoundsTree
* bounds_tree
= bounds_tree_
.get();
2116 View
* paint_root
= this;
2117 while (!bounds_tree
&& !paint_root
->IsPaintRoot()) {
2118 // Assumption is that if IsPaintRoot() is false then parent_ is valid.
2120 paint_root
= paint_root
->parent_
;
2121 bounds_tree
= paint_root
->bounds_tree_
.get();
2127 // Transformations -------------------------------------------------------------
2129 bool View::GetTransformRelativeTo(const View
* ancestor
,
2130 gfx::Transform
* transform
) const {
2131 const View
* p
= this;
2133 while (p
&& p
!= ancestor
) {
2134 transform
->ConcatTransform(p
->GetTransform());
2135 gfx::Transform translation
;
2136 translation
.Translate(static_cast<float>(p
->GetMirroredX()),
2137 static_cast<float>(p
->y()));
2138 transform
->ConcatTransform(translation
);
2143 return p
== ancestor
;
2146 // Coordinate conversion -------------------------------------------------------
2148 bool View::ConvertPointForAncestor(const View
* ancestor
,
2149 gfx::Point
* point
) const {
2150 gfx::Transform trans
;
2151 // TODO(sad): Have some way of caching the transformation results.
2152 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2153 gfx::Point3F
p(*point
);
2154 trans
.TransformPoint(&p
);
2155 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2159 bool View::ConvertPointFromAncestor(const View
* ancestor
,
2160 gfx::Point
* point
) const {
2161 gfx::Transform trans
;
2162 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2163 gfx::Point3F
p(*point
);
2164 trans
.TransformPointReverse(&p
);
2165 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2169 bool View::ConvertRectForAncestor(const View
* ancestor
,
2170 gfx::RectF
* rect
) const {
2171 gfx::Transform trans
;
2172 // TODO(sad): Have some way of caching the transformation results.
2173 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2174 trans
.TransformRect(rect
);
2178 bool View::ConvertRectFromAncestor(const View
* ancestor
,
2179 gfx::RectF
* rect
) const {
2180 gfx::Transform trans
;
2181 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2182 trans
.TransformRectReverse(rect
);
2186 // Accelerated painting --------------------------------------------------------
2188 void View::CreateLayer() {
2189 // A new layer is being created for the view. So all the layers of the
2190 // sub-tree can inherit the visibility of the corresponding view.
2191 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2192 child_at(i
)->UpdateChildLayerVisibility(true);
2194 SetLayer(new ui::Layer());
2195 layer()->set_delegate(this);
2196 #if !defined(NDEBUG)
2197 layer()->set_name(GetClassName());
2200 UpdateParentLayers();
2201 UpdateLayerVisibility();
2203 // The new layer needs to be ordered in the layer tree according
2204 // to the view tree. Children of this layer were added in order
2205 // in UpdateParentLayers().
2207 parent()->ReorderLayers();
2209 Widget
* widget
= GetWidget();
2211 widget
->UpdateRootLayers();
2214 void View::UpdateParentLayers() {
2215 // Attach all top-level un-parented layers.
2216 if (layer() && !layer()->parent()) {
2217 UpdateParentLayer();
2219 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2220 child_at(i
)->UpdateParentLayers();
2224 void View::OrphanLayers() {
2226 if (layer()->parent())
2227 layer()->parent()->Remove(layer());
2229 // The layer belonging to this View has already been orphaned. It is not
2230 // necessary to orphan the child layers.
2233 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2234 child_at(i
)->OrphanLayers();
2237 void View::ReparentLayer(const gfx::Vector2d
& offset
, ui::Layer
* parent_layer
) {
2238 layer()->SetBounds(GetLocalBounds() + offset
);
2239 DCHECK_NE(layer(), parent_layer
);
2241 parent_layer
->Add(layer());
2242 layer()->SchedulePaint(GetLocalBounds());
2243 MoveLayerToParent(layer(), gfx::Point());
2246 void View::DestroyLayer() {
2247 ui::Layer
* new_parent
= layer()->parent();
2248 std::vector
<ui::Layer
*> children
= layer()->children();
2249 for (size_t i
= 0; i
< children
.size(); ++i
) {
2250 layer()->Remove(children
[i
]);
2252 new_parent
->Add(children
[i
]);
2255 LayerOwner::DestroyLayer();
2260 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
2264 Widget
* widget
= GetWidget();
2266 widget
->UpdateRootLayers();
2269 // Input -----------------------------------------------------------------------
2271 bool View::ProcessMousePressed(const ui::MouseEvent
& event
) {
2272 int drag_operations
=
2273 (enabled_
&& event
.IsOnlyLeftMouseButton() &&
2274 HitTestPoint(event
.location())) ?
2275 GetDragOperations(event
.location()) : 0;
2276 ContextMenuController
* context_menu_controller
= event
.IsRightMouseButton() ?
2277 context_menu_controller_
: 0;
2278 View::DragInfo
* drag_info
= GetDragInfo();
2280 // TODO(sky): for debugging 360238.
2282 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2283 kContextMenuOnMousePress
&& HitTestPoint(event
.location())) {
2284 ViewStorage
* view_storage
= ViewStorage::GetInstance();
2285 storage_id
= view_storage
->CreateStorageID();
2286 view_storage
->StoreView(storage_id
, this);
2289 const bool enabled
= enabled_
;
2290 const bool result
= OnMousePressed(event
);
2295 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2296 kContextMenuOnMousePress
) {
2297 // Assume that if there is a context menu controller we won't be deleted
2298 // from mouse pressed.
2299 gfx::Point
location(event
.location());
2300 if (HitTestPoint(location
)) {
2301 if (storage_id
!= 0)
2302 CHECK_EQ(this, ViewStorage::GetInstance()->RetrieveView(storage_id
));
2303 ConvertPointToScreen(this, &location
);
2304 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2309 // WARNING: we may have been deleted, don't use any View variables.
2310 if (drag_operations
!= ui::DragDropTypes::DRAG_NONE
) {
2311 drag_info
->PossibleDrag(event
.location());
2314 return !!context_menu_controller
|| result
;
2317 bool View::ProcessMouseDragged(const ui::MouseEvent
& event
) {
2318 // Copy the field, that way if we're deleted after drag and drop no harm is
2320 ContextMenuController
* context_menu_controller
= context_menu_controller_
;
2321 const bool possible_drag
= GetDragInfo()->possible_drag
;
2322 if (possible_drag
&&
2323 ExceededDragThreshold(GetDragInfo()->start_pt
- event
.location()) &&
2324 (!drag_controller_
||
2325 drag_controller_
->CanStartDragForView(
2326 this, GetDragInfo()->start_pt
, event
.location()))) {
2327 DoDrag(event
, GetDragInfo()->start_pt
,
2328 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
);
2330 if (OnMouseDragged(event
))
2332 // Fall through to return value based on context menu controller.
2334 // WARNING: we may have been deleted.
2335 return (context_menu_controller
!= NULL
) || possible_drag
;
2338 void View::ProcessMouseReleased(const ui::MouseEvent
& event
) {
2339 if (!kContextMenuOnMousePress
&& context_menu_controller_
&&
2340 event
.IsOnlyRightMouseButton()) {
2341 // Assume that if there is a context menu controller we won't be deleted
2342 // from mouse released.
2343 gfx::Point
location(event
.location());
2344 OnMouseReleased(event
);
2345 if (HitTestPoint(location
)) {
2346 ConvertPointToScreen(this, &location
);
2347 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2350 OnMouseReleased(event
);
2352 // WARNING: we may have been deleted.
2355 // Accelerators ----------------------------------------------------------------
2357 void View::RegisterPendingAccelerators() {
2358 if (!accelerators_
.get() ||
2359 registered_accelerator_count_
== accelerators_
->size()) {
2360 // No accelerators are waiting for registration.
2365 // The view is not yet attached to a widget, defer registration until then.
2369 accelerator_focus_manager_
= GetFocusManager();
2370 if (!accelerator_focus_manager_
) {
2371 // Some crash reports seem to show that we may get cases where we have no
2372 // focus manager (see bug #1291225). This should never be the case, just
2373 // making sure we don't crash.
2377 for (std::vector
<ui::Accelerator
>::const_iterator
i(
2378 accelerators_
->begin() + registered_accelerator_count_
);
2379 i
!= accelerators_
->end(); ++i
) {
2380 accelerator_focus_manager_
->RegisterAccelerator(
2381 *i
, ui::AcceleratorManager::kNormalPriority
, this);
2383 registered_accelerator_count_
= accelerators_
->size();
2386 void View::UnregisterAccelerators(bool leave_data_intact
) {
2387 if (!accelerators_
.get())
2391 if (accelerator_focus_manager_
) {
2392 accelerator_focus_manager_
->UnregisterAccelerators(this);
2393 accelerator_focus_manager_
= NULL
;
2395 if (!leave_data_intact
) {
2396 accelerators_
->clear();
2397 accelerators_
.reset();
2399 registered_accelerator_count_
= 0;
2403 // Focus -----------------------------------------------------------------------
2405 void View::InitFocusSiblings(View
* v
, int index
) {
2406 int count
= child_count();
2409 v
->next_focusable_view_
= NULL
;
2410 v
->previous_focusable_view_
= NULL
;
2412 if (index
== count
) {
2413 // We are inserting at the end, but the end of the child list may not be
2414 // the last focusable element. Let's try to find an element with no next
2415 // focusable element to link to.
2416 View
* last_focusable_view
= NULL
;
2417 for (Views::iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2418 if (!(*i
)->next_focusable_view_
) {
2419 last_focusable_view
= *i
;
2423 if (last_focusable_view
== NULL
) {
2424 // Hum... there is a cycle in the focus list. Let's just insert ourself
2425 // after the last child.
2426 View
* prev
= children_
[index
- 1];
2427 v
->previous_focusable_view_
= prev
;
2428 v
->next_focusable_view_
= prev
->next_focusable_view_
;
2429 prev
->next_focusable_view_
->previous_focusable_view_
= v
;
2430 prev
->next_focusable_view_
= v
;
2432 last_focusable_view
->next_focusable_view_
= v
;
2433 v
->next_focusable_view_
= NULL
;
2434 v
->previous_focusable_view_
= last_focusable_view
;
2437 View
* prev
= children_
[index
]->GetPreviousFocusableView();
2438 v
->previous_focusable_view_
= prev
;
2439 v
->next_focusable_view_
= children_
[index
];
2441 prev
->next_focusable_view_
= v
;
2442 children_
[index
]->previous_focusable_view_
= v
;
2447 // System events ---------------------------------------------------------------
2449 void View::PropagateThemeChanged() {
2450 for (int i
= child_count() - 1; i
>= 0; --i
)
2451 child_at(i
)->PropagateThemeChanged();
2455 void View::PropagateLocaleChanged() {
2456 for (int i
= child_count() - 1; i
>= 0; --i
)
2457 child_at(i
)->PropagateLocaleChanged();
2461 // Tooltips --------------------------------------------------------------------
2463 void View::UpdateTooltip() {
2464 Widget
* widget
= GetWidget();
2465 // TODO(beng): The TooltipManager NULL check can be removed when we
2466 // consolidate Init() methods and make views_unittests Init() all
2467 // Widgets that it uses.
2468 if (widget
&& widget
->GetTooltipManager())
2469 widget
->GetTooltipManager()->UpdateTooltip();
2472 // Drag and drop ---------------------------------------------------------------
2474 bool View::DoDrag(const ui::LocatedEvent
& event
,
2475 const gfx::Point
& press_pt
,
2476 ui::DragDropTypes::DragEventSource source
) {
2477 int drag_operations
= GetDragOperations(press_pt
);
2478 if (drag_operations
== ui::DragDropTypes::DRAG_NONE
)
2481 Widget
* widget
= GetWidget();
2482 // We should only start a drag from an event, implying we have a widget.
2485 // Don't attempt to start a drag while in the process of dragging. This is
2486 // especially important on X where we can get multiple mouse move events when
2487 // we start the drag.
2488 if (widget
->dragged_view())
2491 OSExchangeData data
;
2492 WriteDragData(press_pt
, &data
);
2494 // Message the RootView to do the drag and drop. That way if we're removed
2495 // the RootView can detect it and avoid calling us back.
2496 gfx::Point
widget_location(event
.location());
2497 ConvertPointToWidget(this, &widget_location
);
2498 widget
->RunShellDrag(this, data
, widget_location
, drag_operations
, source
);
2499 // WARNING: we may have been deleted.
2503 } // namespace views