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 gfx::RTree(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_
->Query(canvas_bounds
, damaged_views
.get());
802 // Construct a CullSet to wrap the damaged views set, it will delete it
803 // for us on scope exit.
804 CullSet
paint_root_cull_set(damaged_views
.Pass());
805 // Paint all descendents using our new cull set.
806 PaintCommon(canvas
, paint_root_cull_set
);
808 // Not a paint root, so we can proceed as normal.
809 PaintCommon(canvas
, cull_set
);
814 void View::set_background(Background
* b
) {
815 background_
.reset(b
);
818 void View::SetBorder(scoped_ptr
<Border
> b
) { border_
= b
.Pass(); }
820 ui::ThemeProvider
* View::GetThemeProvider() const {
821 const Widget
* widget
= GetWidget();
822 return widget
? widget
->GetThemeProvider() : NULL
;
825 const ui::NativeTheme
* View::GetNativeTheme() const {
826 const Widget
* widget
= GetWidget();
827 return widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
830 // Input -----------------------------------------------------------------------
832 View
* View::GetEventHandlerForPoint(const gfx::Point
& point
) {
833 return GetEventHandlerForRect(gfx::Rect(point
, gfx::Size(1, 1)));
836 View
* View::GetEventHandlerForRect(const gfx::Rect
& rect
) {
837 // |rect_view| represents the current best candidate to return
838 // if rect-based targeting (i.e., fuzzing) is used.
839 // |rect_view_distance| is used to keep track of the distance
840 // between the center point of |rect_view| and the center
842 View
* rect_view
= NULL
;
843 int rect_view_distance
= INT_MAX
;
845 // |point_view| represents the view that would have been returned
846 // from this function call if point-based targeting were used.
847 View
* point_view
= NULL
;
849 for (int i
= child_count() - 1; i
>= 0; --i
) {
850 View
* child
= child_at(i
);
852 if (!child
->CanProcessEventsWithinSubtree())
855 // Ignore any children which are invisible or do not intersect |rect|.
856 if (!child
->visible())
858 gfx::RectF
rect_in_child_coords_f(rect
);
859 ConvertRectToTarget(this, child
, &rect_in_child_coords_f
);
860 gfx::Rect rect_in_child_coords
= gfx::ToEnclosingRect(
861 rect_in_child_coords_f
);
862 if (!child
->HitTestRect(rect_in_child_coords
))
865 View
* cur_view
= child
->GetEventHandlerForRect(rect_in_child_coords
);
867 if (views::UsePointBasedTargeting(rect
))
870 gfx::RectF
cur_view_bounds_f(cur_view
->GetLocalBounds());
871 ConvertRectToTarget(cur_view
, this, &cur_view_bounds_f
);
872 gfx::Rect cur_view_bounds
= gfx::ToEnclosingRect(
874 if (views::PercentCoveredBy(cur_view_bounds
, rect
) >= kRectTargetOverlap
) {
875 // |cur_view| is a suitable candidate for rect-based targeting.
876 // Check to see if it is the closest suitable candidate so far.
877 gfx::Point
touch_center(rect
.CenterPoint());
878 int cur_dist
= views::DistanceSquaredFromCenterToPoint(touch_center
,
880 if (!rect_view
|| cur_dist
< rect_view_distance
) {
881 rect_view
= cur_view
;
882 rect_view_distance
= cur_dist
;
884 } else if (!rect_view
&& !point_view
) {
885 // Rect-based targeting has not yielded any candidates so far. Check
886 // if point-based targeting would have selected |cur_view|.
887 gfx::Point
point_in_child_coords(rect_in_child_coords
.CenterPoint());
888 if (child
->HitTestPoint(point_in_child_coords
))
889 point_view
= child
->GetEventHandlerForPoint(point_in_child_coords
);
893 if (views::UsePointBasedTargeting(rect
) || (!rect_view
&& !point_view
))
896 // If |this| is a suitable candidate for rect-based targeting, check to
897 // see if it is closer than the current best suitable candidate so far.
898 gfx::Rect
local_bounds(GetLocalBounds());
899 if (views::PercentCoveredBy(local_bounds
, rect
) >= kRectTargetOverlap
) {
900 gfx::Point
touch_center(rect
.CenterPoint());
901 int cur_dist
= views::DistanceSquaredFromCenterToPoint(touch_center
,
903 if (!rect_view
|| cur_dist
< rect_view_distance
)
907 return rect_view
? rect_view
: point_view
;
910 bool View::CanProcessEventsWithinSubtree() const {
914 View
* View::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
915 if (!HitTestPoint(point
))
918 // Walk the child Views recursively looking for the View that most
919 // tightly encloses the specified point.
920 for (int i
= child_count() - 1; i
>= 0; --i
) {
921 View
* child
= child_at(i
);
922 if (!child
->visible())
925 if (!child
->CanProcessEventsWithinSubtree())
928 gfx::Point
point_in_child_coords(point
);
929 ConvertPointToTarget(this, child
, &point_in_child_coords
);
930 View
* handler
= child
->GetTooltipHandlerForPoint(point_in_child_coords
);
937 gfx::NativeCursor
View::GetCursor(const ui::MouseEvent
& event
) {
939 static ui::Cursor arrow
;
940 if (!arrow
.platform())
941 arrow
.SetPlatformCursor(LoadCursor(NULL
, IDC_ARROW
));
944 return gfx::kNullCursor
;
948 bool View::HitTestPoint(const gfx::Point
& point
) const {
949 return HitTestRect(gfx::Rect(point
, gfx::Size(1, 1)));
952 bool View::HitTestRect(const gfx::Rect
& rect
) const {
953 if (GetLocalBounds().Intersects(rect
)) {
954 if (HasHitTestMask()) {
956 HitTestSource source
= HIT_TEST_SOURCE_MOUSE
;
957 if (!views::UsePointBasedTargeting(rect
))
958 source
= HIT_TEST_SOURCE_TOUCH
;
959 GetHitTestMask(source
, &mask
);
960 SkRegion clip_region
;
961 clip_region
.setRect(0, 0, width(), height());
962 SkRegion mask_region
;
963 return mask_region
.setPath(mask
, clip_region
) &&
964 mask_region
.intersects(RectToSkIRect(rect
));
966 // No mask, but inside our bounds.
969 // Outside our bounds.
973 bool View::IsMouseHovered() {
974 // If we haven't yet been placed in an onscreen view hierarchy, we can't be
979 // If mouse events are disabled, then the mouse cursor is invisible and
980 // is therefore not hovering over this button.
981 if (!GetWidget()->IsMouseEventsEnabled())
984 gfx::Point
cursor_pos(gfx::Screen::GetScreenFor(
985 GetWidget()->GetNativeView())->GetCursorScreenPoint());
986 ConvertPointFromScreen(this, &cursor_pos
);
987 return HitTestPoint(cursor_pos
);
990 bool View::OnMousePressed(const ui::MouseEvent
& event
) {
994 bool View::OnMouseDragged(const ui::MouseEvent
& event
) {
998 void View::OnMouseReleased(const ui::MouseEvent
& event
) {
1001 void View::OnMouseCaptureLost() {
1004 void View::OnMouseMoved(const ui::MouseEvent
& event
) {
1007 void View::OnMouseEntered(const ui::MouseEvent
& event
) {
1010 void View::OnMouseExited(const ui::MouseEvent
& event
) {
1013 void View::SetMouseHandler(View
* new_mouse_handler
) {
1014 // |new_mouse_handler| may be NULL.
1016 parent_
->SetMouseHandler(new_mouse_handler
);
1019 bool View::OnKeyPressed(const ui::KeyEvent
& event
) {
1023 bool View::OnKeyReleased(const ui::KeyEvent
& event
) {
1027 bool View::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
1031 void View::OnKeyEvent(ui::KeyEvent
* event
) {
1032 bool consumed
= (event
->type() == ui::ET_KEY_PRESSED
) ? OnKeyPressed(*event
) :
1033 OnKeyReleased(*event
);
1035 event
->StopPropagation();
1038 void View::OnMouseEvent(ui::MouseEvent
* event
) {
1039 switch (event
->type()) {
1040 case ui::ET_MOUSE_PRESSED
:
1041 if (ProcessMousePressed(*event
))
1042 event
->SetHandled();
1045 case ui::ET_MOUSE_MOVED
:
1046 if ((event
->flags() & (ui::EF_LEFT_MOUSE_BUTTON
|
1047 ui::EF_RIGHT_MOUSE_BUTTON
|
1048 ui::EF_MIDDLE_MOUSE_BUTTON
)) == 0) {
1049 OnMouseMoved(*event
);
1053 case ui::ET_MOUSE_DRAGGED
:
1054 if (ProcessMouseDragged(*event
))
1055 event
->SetHandled();
1058 case ui::ET_MOUSE_RELEASED
:
1059 ProcessMouseReleased(*event
);
1062 case ui::ET_MOUSEWHEEL
:
1063 if (OnMouseWheel(*static_cast<ui::MouseWheelEvent
*>(event
)))
1064 event
->SetHandled();
1067 case ui::ET_MOUSE_ENTERED
:
1068 if (event
->flags() & ui::EF_TOUCH_ACCESSIBILITY
)
1069 NotifyAccessibilityEvent(ui::AX_EVENT_HOVER
, true);
1070 OnMouseEntered(*event
);
1073 case ui::ET_MOUSE_EXITED
:
1074 OnMouseExited(*event
);
1082 void View::OnScrollEvent(ui::ScrollEvent
* event
) {
1085 void View::OnTouchEvent(ui::TouchEvent
* event
) {
1086 NOTREACHED() << "Views should not receive touch events.";
1089 void View::OnGestureEvent(ui::GestureEvent
* event
) {
1092 ui::TextInputClient
* View::GetTextInputClient() {
1096 InputMethod
* View::GetInputMethod() {
1097 Widget
* widget
= GetWidget();
1098 return widget
? widget
->GetInputMethod() : NULL
;
1101 const InputMethod
* View::GetInputMethod() const {
1102 const Widget
* widget
= GetWidget();
1103 return widget
? widget
->GetInputMethod() : NULL
;
1106 scoped_ptr
<ui::EventTargeter
>
1107 View::SetEventTargeter(scoped_ptr
<ui::EventTargeter
> targeter
) {
1108 scoped_ptr
<ui::EventTargeter
> old_targeter
= targeter_
.Pass();
1109 targeter_
= targeter
.Pass();
1110 return old_targeter
.Pass();
1113 bool View::CanAcceptEvent(const ui::Event
& event
) {
1117 ui::EventTarget
* View::GetParentTarget() {
1121 scoped_ptr
<ui::EventTargetIterator
> View::GetChildIterator() const {
1122 return scoped_ptr
<ui::EventTargetIterator
>(
1123 new ui::EventTargetIteratorImpl
<View
>(children_
));
1126 ui::EventTargeter
* View::GetEventTargeter() {
1127 return targeter_
.get();
1130 const ui::EventTargeter
* View::GetEventTargeter() const {
1131 return targeter_
.get();
1134 void View::ConvertEventToTarget(ui::EventTarget
* target
,
1135 ui::LocatedEvent
* event
) {
1136 event
->ConvertLocationToTarget(this, static_cast<View
*>(target
));
1139 // Accelerators ----------------------------------------------------------------
1141 void View::AddAccelerator(const ui::Accelerator
& accelerator
) {
1142 if (!accelerators_
.get())
1143 accelerators_
.reset(new std::vector
<ui::Accelerator
>());
1145 if (std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
) ==
1146 accelerators_
->end()) {
1147 accelerators_
->push_back(accelerator
);
1149 RegisterPendingAccelerators();
1152 void View::RemoveAccelerator(const ui::Accelerator
& accelerator
) {
1153 if (!accelerators_
.get()) {
1154 NOTREACHED() << "Removing non-existing accelerator";
1158 std::vector
<ui::Accelerator
>::iterator
i(
1159 std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
));
1160 if (i
== accelerators_
->end()) {
1161 NOTREACHED() << "Removing non-existing accelerator";
1165 size_t index
= i
- accelerators_
->begin();
1166 accelerators_
->erase(i
);
1167 if (index
>= registered_accelerator_count_
) {
1168 // The accelerator is not registered to FocusManager.
1171 --registered_accelerator_count_
;
1173 // Providing we are attached to a Widget and registered with a focus manager,
1174 // we should de-register from that focus manager now.
1175 if (GetWidget() && accelerator_focus_manager_
)
1176 accelerator_focus_manager_
->UnregisterAccelerator(accelerator
, this);
1179 void View::ResetAccelerators() {
1180 if (accelerators_
.get())
1181 UnregisterAccelerators(false);
1184 bool View::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1188 bool View::CanHandleAccelerators() const {
1189 return enabled() && IsDrawn() && GetWidget() && GetWidget()->IsVisible();
1192 // Focus -----------------------------------------------------------------------
1194 bool View::HasFocus() const {
1195 const FocusManager
* focus_manager
= GetFocusManager();
1196 return focus_manager
&& (focus_manager
->GetFocusedView() == this);
1199 View
* View::GetNextFocusableView() {
1200 return next_focusable_view_
;
1203 const View
* View::GetNextFocusableView() const {
1204 return next_focusable_view_
;
1207 View
* View::GetPreviousFocusableView() {
1208 return previous_focusable_view_
;
1211 void View::SetNextFocusableView(View
* view
) {
1213 view
->previous_focusable_view_
= this;
1214 next_focusable_view_
= view
;
1217 void View::SetFocusable(bool focusable
) {
1218 if (focusable_
== focusable
)
1221 focusable_
= focusable
;
1224 bool View::IsFocusable() const {
1225 return focusable_
&& enabled_
&& IsDrawn();
1228 bool View::IsAccessibilityFocusable() const {
1229 return (focusable_
|| accessibility_focusable_
) && enabled_
&& IsDrawn();
1232 void View::SetAccessibilityFocusable(bool accessibility_focusable
) {
1233 if (accessibility_focusable_
== accessibility_focusable
)
1236 accessibility_focusable_
= accessibility_focusable
;
1239 FocusManager
* View::GetFocusManager() {
1240 Widget
* widget
= GetWidget();
1241 return widget
? widget
->GetFocusManager() : NULL
;
1244 const FocusManager
* View::GetFocusManager() const {
1245 const Widget
* widget
= GetWidget();
1246 return widget
? widget
->GetFocusManager() : NULL
;
1249 void View::RequestFocus() {
1250 FocusManager
* focus_manager
= GetFocusManager();
1251 if (focus_manager
&& IsFocusable())
1252 focus_manager
->SetFocusedView(this);
1255 bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) {
1259 FocusTraversable
* View::GetFocusTraversable() {
1263 FocusTraversable
* View::GetPaneFocusTraversable() {
1267 // Tooltips --------------------------------------------------------------------
1269 bool View::GetTooltipText(const gfx::Point
& p
, base::string16
* tooltip
) const {
1273 bool View::GetTooltipTextOrigin(const gfx::Point
& p
, gfx::Point
* loc
) const {
1277 // Context menus ---------------------------------------------------------------
1279 void View::ShowContextMenu(const gfx::Point
& p
,
1280 ui::MenuSourceType source_type
) {
1281 if (!context_menu_controller_
)
1284 context_menu_controller_
->ShowContextMenuForView(this, p
, source_type
);
1288 bool View::ShouldShowContextMenuOnMousePress() {
1289 return kContextMenuOnMousePress
;
1292 // Drag and drop ---------------------------------------------------------------
1294 bool View::GetDropFormats(
1296 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
1300 bool View::AreDropTypesRequired() {
1304 bool View::CanDrop(const OSExchangeData
& data
) {
1305 // TODO(sky): when I finish up migration, this should default to true.
1309 void View::OnDragEntered(const ui::DropTargetEvent
& event
) {
1312 int View::OnDragUpdated(const ui::DropTargetEvent
& event
) {
1313 return ui::DragDropTypes::DRAG_NONE
;
1316 void View::OnDragExited() {
1319 int View::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1320 return ui::DragDropTypes::DRAG_NONE
;
1323 void View::OnDragDone() {
1327 bool View::ExceededDragThreshold(const gfx::Vector2d
& delta
) {
1328 return (abs(delta
.x()) > GetHorizontalDragThreshold() ||
1329 abs(delta
.y()) > GetVerticalDragThreshold());
1332 // Accessibility----------------------------------------------------------------
1334 gfx::NativeViewAccessible
View::GetNativeViewAccessible() {
1335 if (!native_view_accessibility_
)
1336 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1337 if (native_view_accessibility_
)
1338 return native_view_accessibility_
->GetNativeObject();
1342 void View::NotifyAccessibilityEvent(
1343 ui::AXEvent event_type
,
1344 bool send_native_event
) {
1345 if (ViewsDelegate::views_delegate
)
1346 ViewsDelegate::views_delegate
->NotifyAccessibilityEvent(this, event_type
);
1348 if (send_native_event
&& GetWidget()) {
1349 if (!native_view_accessibility_
)
1350 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1351 if (native_view_accessibility_
)
1352 native_view_accessibility_
->NotifyAccessibilityEvent(event_type
);
1356 // Scrolling -------------------------------------------------------------------
1358 void View::ScrollRectToVisible(const gfx::Rect
& rect
) {
1359 // We must take RTL UI mirroring into account when adjusting the position of
1362 gfx::Rect
scroll_rect(rect
);
1363 scroll_rect
.Offset(GetMirroredX(), y());
1364 parent_
->ScrollRectToVisible(scroll_rect
);
1368 int View::GetPageScrollIncrement(ScrollView
* scroll_view
,
1369 bool is_horizontal
, bool is_positive
) {
1373 int View::GetLineScrollIncrement(ScrollView
* scroll_view
,
1374 bool is_horizontal
, bool is_positive
) {
1378 ////////////////////////////////////////////////////////////////////////////////
1381 // Size and disposition --------------------------------------------------------
1383 void View::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1386 void View::PreferredSizeChanged() {
1389 parent_
->ChildPreferredSizeChanged(this);
1392 bool View::NeedsNotificationWhenVisibleBoundsChange() const {
1396 void View::OnVisibleBoundsChanged() {
1399 // Tree operations -------------------------------------------------------------
1401 void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails
& details
) {
1404 void View::VisibilityChanged(View
* starting_from
, bool is_visible
) {
1407 void View::NativeViewHierarchyChanged() {
1408 FocusManager
* focus_manager
= GetFocusManager();
1409 if (accelerator_focus_manager_
!= focus_manager
) {
1410 UnregisterAccelerators(true);
1413 RegisterPendingAccelerators();
1417 // Painting --------------------------------------------------------------------
1419 void View::PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1420 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1421 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1422 if (!child_at(i
)->layer())
1423 child_at(i
)->Paint(canvas
, cull_set
);
1426 void View::OnPaint(gfx::Canvas
* canvas
) {
1427 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1428 OnPaintBackground(canvas
);
1429 OnPaintBorder(canvas
);
1432 void View::OnPaintBackground(gfx::Canvas
* canvas
) {
1433 if (background_
.get()) {
1434 TRACE_EVENT2("views", "View::OnPaintBackground",
1435 "width", canvas
->sk_canvas()->getDevice()->width(),
1436 "height", canvas
->sk_canvas()->getDevice()->height());
1437 background_
->Paint(canvas
, this);
1441 void View::OnPaintBorder(gfx::Canvas
* canvas
) {
1442 if (border_
.get()) {
1443 TRACE_EVENT2("views", "View::OnPaintBorder",
1444 "width", canvas
->sk_canvas()->getDevice()->width(),
1445 "height", canvas
->sk_canvas()->getDevice()->height());
1446 border_
->Paint(*this, canvas
);
1450 bool View::IsPaintRoot() {
1451 return paint_to_layer_
|| !parent_
;
1454 // Accelerated Painting --------------------------------------------------------
1456 void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely
) {
1457 // This method should not have the side-effect of creating the layer.
1459 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely
);
1462 gfx::Vector2d
View::CalculateOffsetToAncestorWithLayer(
1463 ui::Layer
** layer_parent
) {
1466 *layer_parent
= layer();
1467 return gfx::Vector2d();
1470 return gfx::Vector2d();
1472 return gfx::Vector2d(GetMirroredX(), y()) +
1473 parent_
->CalculateOffsetToAncestorWithLayer(layer_parent
);
1476 void View::UpdateParentLayer() {
1480 ui::Layer
* parent_layer
= NULL
;
1481 gfx::Vector2d
offset(GetMirroredX(), y());
1484 offset
+= parent_
->CalculateOffsetToAncestorWithLayer(&parent_layer
);
1486 ReparentLayer(offset
, parent_layer
);
1489 void View::MoveLayerToParent(ui::Layer
* parent_layer
,
1490 const gfx::Point
& point
) {
1491 gfx::Point
local_point(point
);
1492 if (parent_layer
!= layer())
1493 local_point
.Offset(GetMirroredX(), y());
1494 if (layer() && parent_layer
!= layer()) {
1495 parent_layer
->Add(layer());
1496 SetLayerBounds(gfx::Rect(local_point
.x(), local_point
.y(),
1497 width(), height()));
1499 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1500 child_at(i
)->MoveLayerToParent(parent_layer
, local_point
);
1504 void View::UpdateLayerVisibility() {
1505 bool visible
= visible_
;
1506 for (const View
* v
= parent_
; visible
&& v
&& !v
->layer(); v
= v
->parent_
)
1507 visible
= v
->visible();
1509 UpdateChildLayerVisibility(visible
);
1512 void View::UpdateChildLayerVisibility(bool ancestor_visible
) {
1514 layer()->SetVisible(ancestor_visible
&& visible_
);
1516 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1517 child_at(i
)->UpdateChildLayerVisibility(ancestor_visible
&& visible_
);
1521 void View::UpdateChildLayerBounds(const gfx::Vector2d
& offset
) {
1523 SetLayerBounds(GetLocalBounds() + offset
);
1525 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
1526 View
* child
= child_at(i
);
1527 child
->UpdateChildLayerBounds(
1528 offset
+ gfx::Vector2d(child
->GetMirroredX(), child
->y()));
1533 void View::OnPaintLayer(gfx::Canvas
* canvas
) {
1534 if (!layer() || !layer()->fills_bounds_opaquely())
1535 canvas
->DrawColor(SK_ColorBLACK
, SkXfermode::kClear_Mode
);
1536 PaintCommon(canvas
, CullSet());
1539 void View::OnDeviceScaleFactorChanged(float device_scale_factor
) {
1540 // Repainting with new scale factor will paint the content at the right scale.
1543 base::Closure
View::PrepareForLayerBoundsChange() {
1544 return base::Closure();
1547 void View::ReorderLayers() {
1549 while (v
&& !v
->layer())
1552 Widget
* widget
= GetWidget();
1555 ui::Layer
* layer
= widget
->GetLayer();
1557 widget
->GetRootView()->ReorderChildLayers(layer
);
1560 v
->ReorderChildLayers(v
->layer());
1564 // Reorder the widget's child NativeViews in case a child NativeView is
1565 // associated with a view (eg via a NativeViewHost). Always do the
1566 // reordering because the associated NativeView's layer (if it has one)
1567 // is parented to the widget's layer regardless of whether the host view has
1568 // an ancestor with a layer.
1569 widget
->ReorderNativeViews();
1573 void View::ReorderChildLayers(ui::Layer
* parent_layer
) {
1574 if (layer() && layer() != parent_layer
) {
1575 DCHECK_EQ(parent_layer
, layer()->parent());
1576 parent_layer
->StackAtBottom(layer());
1578 // Iterate backwards through the children so that a child with a layer
1579 // which is further to the back is stacked above one which is further to
1581 for (Views::reverse_iterator
it(children_
.rbegin());
1582 it
!= children_
.rend(); ++it
) {
1583 (*it
)->ReorderChildLayers(parent_layer
);
1588 // Input -----------------------------------------------------------------------
1590 bool View::HasHitTestMask() const {
1594 void View::GetHitTestMask(HitTestSource source
, gfx::Path
* mask
) const {
1598 View::DragInfo
* View::GetDragInfo() {
1599 return parent_
? parent_
->GetDragInfo() : NULL
;
1602 // Focus -----------------------------------------------------------------------
1604 void View::OnFocus() {
1605 // TODO(beng): Investigate whether it's possible for us to move this to
1607 // By default, we clear the native focus. This ensures that no visible native
1608 // view as the focus and that we still receive keyboard inputs.
1609 FocusManager
* focus_manager
= GetFocusManager();
1611 focus_manager
->ClearNativeFocus();
1613 // TODO(beng): Investigate whether it's possible for us to move this to
1615 // Notify assistive technologies of the focus change.
1616 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
1619 void View::OnBlur() {
1622 void View::Focus() {
1630 // Tooltips --------------------------------------------------------------------
1632 void View::TooltipTextChanged() {
1633 Widget
* widget
= GetWidget();
1634 // TooltipManager may be null if there is a problem creating it.
1635 if (widget
&& widget
->GetTooltipManager())
1636 widget
->GetTooltipManager()->TooltipTextChanged(this);
1639 // Context menus ---------------------------------------------------------------
1641 gfx::Point
View::GetKeyboardContextMenuLocation() {
1642 gfx::Rect vis_bounds
= GetVisibleBounds();
1643 gfx::Point
screen_point(vis_bounds
.x() + vis_bounds
.width() / 2,
1644 vis_bounds
.y() + vis_bounds
.height() / 2);
1645 ConvertPointToScreen(this, &screen_point
);
1646 return screen_point
;
1649 // Drag and drop ---------------------------------------------------------------
1651 int View::GetDragOperations(const gfx::Point
& press_pt
) {
1652 return drag_controller_
?
1653 drag_controller_
->GetDragOperationsForView(this, press_pt
) :
1654 ui::DragDropTypes::DRAG_NONE
;
1657 void View::WriteDragData(const gfx::Point
& press_pt
, OSExchangeData
* data
) {
1658 DCHECK(drag_controller_
);
1659 drag_controller_
->WriteDragDataForView(this, press_pt
, data
);
1662 bool View::InDrag() {
1663 Widget
* widget
= GetWidget();
1664 return widget
? widget
->dragged_view() == this : false;
1667 int View::GetHorizontalDragThreshold() {
1668 // TODO(jennyz): This value may need to be adjusted for different platforms
1669 // and for different display density.
1670 return kDefaultHorizontalDragThreshold
;
1673 int View::GetVerticalDragThreshold() {
1674 // TODO(jennyz): This value may need to be adjusted for different platforms
1675 // and for different display density.
1676 return kDefaultVerticalDragThreshold
;
1679 // Debugging -------------------------------------------------------------------
1681 #if !defined(NDEBUG)
1683 std::string
View::PrintViewGraph(bool first
) {
1684 return DoPrintViewGraph(first
, this);
1687 std::string
View::DoPrintViewGraph(bool first
, View
* view_with_children
) {
1688 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1689 const size_t kMaxPointerStringLength
= 19;
1694 result
.append("digraph {\n");
1696 // Node characteristics.
1697 char p
[kMaxPointerStringLength
];
1699 const std::string
class_name(GetClassName());
1700 size_t base_name_index
= class_name
.find_last_of('/');
1701 if (base_name_index
== std::string::npos
)
1702 base_name_index
= 0;
1706 char bounds_buffer
[512];
1708 // Information about current node.
1709 base::snprintf(p
, arraysize(bounds_buffer
), "%p", view_with_children
);
1710 result
.append(" N");
1711 result
.append(p
+ 2);
1712 result
.append(" [label=\"");
1714 result
.append(class_name
.substr(base_name_index
).c_str());
1716 base::snprintf(bounds_buffer
,
1717 arraysize(bounds_buffer
),
1718 "\\n bounds: (%d, %d), (%dx%d)",
1723 result
.append(bounds_buffer
);
1725 gfx::DecomposedTransform decomp
;
1726 if (!GetTransform().IsIdentity() &&
1727 gfx::DecomposeTransform(&decomp
, GetTransform())) {
1728 base::snprintf(bounds_buffer
,
1729 arraysize(bounds_buffer
),
1730 "\\n translation: (%f, %f)",
1731 decomp
.translate
[0],
1732 decomp
.translate
[1]);
1733 result
.append(bounds_buffer
);
1735 base::snprintf(bounds_buffer
,
1736 arraysize(bounds_buffer
),
1737 "\\n rotation: %3.2f",
1738 std::acos(decomp
.quaternion
[3]) * 360.0 / M_PI
);
1739 result
.append(bounds_buffer
);
1741 base::snprintf(bounds_buffer
,
1742 arraysize(bounds_buffer
),
1743 "\\n scale: (%2.4f, %2.4f)",
1746 result
.append(bounds_buffer
);
1749 result
.append("\"");
1751 result
.append(", shape=box");
1753 if (layer()->has_external_content())
1754 result
.append(", color=green");
1756 result
.append(", color=red");
1758 if (layer()->fills_bounds_opaquely())
1759 result
.append(", style=filled");
1761 result
.append("]\n");
1765 char pp
[kMaxPointerStringLength
];
1767 base::snprintf(pp
, kMaxPointerStringLength
, "%p", parent_
);
1768 result
.append(" N");
1769 result
.append(pp
+ 2);
1770 result
.append(" -> N");
1771 result
.append(p
+ 2);
1772 result
.append("\n");
1776 for (int i
= 0, count
= view_with_children
->child_count(); i
< count
; ++i
)
1777 result
.append(view_with_children
->child_at(i
)->PrintViewGraph(false));
1780 result
.append("}\n");
1786 ////////////////////////////////////////////////////////////////////////////////
1789 // DropInfo --------------------------------------------------------------------
1791 void View::DragInfo::Reset() {
1792 possible_drag
= false;
1793 start_pt
= gfx::Point();
1796 void View::DragInfo::PossibleDrag(const gfx::Point
& p
) {
1797 possible_drag
= true;
1801 // Painting --------------------------------------------------------------------
1803 void View::SchedulePaintBoundsChanged(SchedulePaintType type
) {
1804 // If we have a layer and the View's size did not change, we do not need to
1805 // schedule any paints since the layer will be redrawn at its new location
1806 // during the next Draw() cycle in the compositor.
1807 if (!layer() || type
== SCHEDULE_PAINT_SIZE_CHANGED
) {
1808 // Otherwise, if the size changes or we don't have a layer then we need to
1809 // use SchedulePaint to invalidate the area occupied by the View.
1811 } else if (parent_
&& type
== SCHEDULE_PAINT_SIZE_SAME
) {
1812 // The compositor doesn't Draw() until something on screen changes, so
1813 // if our position changes but nothing is being animated on screen, then
1814 // tell the compositor to redraw the scene. We know layer() exists due to
1815 // the above if clause.
1816 layer()->ScheduleDraw();
1820 void View::PaintCommon(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1825 // If the View we are about to paint requested the canvas to be flipped, we
1826 // should change the transform appropriately.
1827 // The canvas mirroring is undone once the View is done painting so that we
1828 // don't pass the canvas with the mirrored transform to Views that didn't
1829 // request the canvas to be flipped.
1830 gfx::ScopedCanvas
scoped(canvas
);
1831 if (FlipCanvasOnPaintForRTLUI()) {
1832 canvas
->Translate(gfx::Vector2d(width(), 0));
1833 canvas
->Scale(-1, 1);
1839 PaintChildren(canvas
, cull_set
);
1842 // Tree operations -------------------------------------------------------------
1844 void View::DoRemoveChildView(View
* view
,
1845 bool update_focus_cycle
,
1846 bool update_tool_tip
,
1847 bool delete_removed_view
,
1851 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
1852 scoped_ptr
<View
> view_to_be_deleted
;
1853 if (i
!= children_
.end()) {
1854 if (update_focus_cycle
) {
1855 // Let's remove the view from the focus traversal.
1856 View
* next_focusable
= view
->next_focusable_view_
;
1857 View
* prev_focusable
= view
->previous_focusable_view_
;
1859 prev_focusable
->next_focusable_view_
= next_focusable
;
1861 next_focusable
->previous_focusable_view_
= prev_focusable
;
1865 UnregisterChildrenForVisibleBoundsNotification(view
);
1866 if (view
->visible())
1867 view
->SchedulePaint();
1868 GetWidget()->NotifyWillRemoveView(view
);
1871 // Remove the bounds of this child and any of its descendants from our
1872 // paint root bounds tree.
1873 gfx::RTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
1875 view
->RemoveRootBounds(bounds_tree
);
1877 view
->PropagateRemoveNotifications(this, new_parent
);
1878 view
->parent_
= NULL
;
1879 view
->UpdateLayerVisibility();
1881 if (delete_removed_view
&& !view
->owned_by_client_
)
1882 view_to_be_deleted
.reset(view
);
1887 if (update_tool_tip
)
1890 if (layout_manager_
.get())
1891 layout_manager_
->ViewRemoved(this, view
);
1894 void View::PropagateRemoveNotifications(View
* old_parent
, View
* new_parent
) {
1895 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1896 child_at(i
)->PropagateRemoveNotifications(old_parent
, new_parent
);
1898 ViewHierarchyChangedDetails
details(false, old_parent
, this, new_parent
);
1899 for (View
* v
= this; v
; v
= v
->parent_
)
1900 v
->ViewHierarchyChangedImpl(true, details
);
1903 void View::PropagateAddNotifications(
1904 const ViewHierarchyChangedDetails
& details
) {
1905 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1906 child_at(i
)->PropagateAddNotifications(details
);
1907 ViewHierarchyChangedImpl(true, details
);
1910 void View::PropagateNativeViewHierarchyChanged() {
1911 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1912 child_at(i
)->PropagateNativeViewHierarchyChanged();
1913 NativeViewHierarchyChanged();
1916 void View::ViewHierarchyChangedImpl(
1917 bool register_accelerators
,
1918 const ViewHierarchyChangedDetails
& details
) {
1919 if (register_accelerators
) {
1920 if (details
.is_add
) {
1921 // If you get this registration, you are part of a subtree that has been
1922 // added to the view hierarchy.
1923 if (GetFocusManager())
1924 RegisterPendingAccelerators();
1926 if (details
.child
== this)
1927 UnregisterAccelerators(true);
1931 if (details
.is_add
&& layer() && !layer()->parent()) {
1932 UpdateParentLayer();
1933 Widget
* widget
= GetWidget();
1935 widget
->UpdateRootLayers();
1936 } else if (!details
.is_add
&& details
.child
== this) {
1937 // Make sure the layers belonging to the subtree rooted at |child| get
1938 // removed from layers that do not belong in the same subtree.
1940 Widget
* widget
= GetWidget();
1942 widget
->UpdateRootLayers();
1945 ViewHierarchyChanged(details
);
1946 details
.parent
->needs_layout_
= true;
1949 void View::PropagateNativeThemeChanged(const ui::NativeTheme
* theme
) {
1950 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1951 child_at(i
)->PropagateNativeThemeChanged(theme
);
1952 OnNativeThemeChanged(theme
);
1955 // Size and disposition --------------------------------------------------------
1957 void View::PropagateVisibilityNotifications(View
* start
, bool is_visible
) {
1958 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1959 child_at(i
)->PropagateVisibilityNotifications(start
, is_visible
);
1960 VisibilityChangedImpl(start
, is_visible
);
1963 void View::VisibilityChangedImpl(View
* starting_from
, bool is_visible
) {
1964 VisibilityChanged(starting_from
, is_visible
);
1967 void View::BoundsChanged(const gfx::Rect
& previous_bounds
) {
1968 // Mark our bounds as dirty for the paint root, also see if we need to
1969 // recompute our children's bounds due to origin change.
1970 bool origin_changed
=
1971 previous_bounds
.OffsetFromOrigin() != bounds_
.OffsetFromOrigin();
1972 SetRootBoundsDirty(origin_changed
);
1975 // Paint the new bounds.
1976 SchedulePaintBoundsChanged(
1977 bounds_
.size() == previous_bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
1978 SCHEDULE_PAINT_SIZE_CHANGED
);
1983 SetLayerBounds(GetLocalBounds() +
1984 gfx::Vector2d(GetMirroredX(), y()) +
1985 parent_
->CalculateOffsetToAncestorWithLayer(NULL
));
1987 SetLayerBounds(bounds_
);
1990 // If our bounds have changed, then any descendant layer bounds may have
1991 // changed. Update them accordingly.
1992 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
1995 OnBoundsChanged(previous_bounds
);
1997 if (previous_bounds
.size() != size()) {
1998 needs_layout_
= false;
2002 if (NeedsNotificationWhenVisibleBoundsChange())
2003 OnVisibleBoundsChanged();
2005 // Notify interested Views that visible bounds within the root view may have
2007 if (descendants_to_notify_
.get()) {
2008 for (Views::iterator
i(descendants_to_notify_
->begin());
2009 i
!= descendants_to_notify_
->end(); ++i
) {
2010 (*i
)->OnVisibleBoundsChanged();
2016 void View::RegisterChildrenForVisibleBoundsNotification(View
* view
) {
2017 if (view
->NeedsNotificationWhenVisibleBoundsChange())
2018 view
->RegisterForVisibleBoundsNotification();
2019 for (int i
= 0; i
< view
->child_count(); ++i
)
2020 RegisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
2024 void View::UnregisterChildrenForVisibleBoundsNotification(View
* view
) {
2025 if (view
->NeedsNotificationWhenVisibleBoundsChange())
2026 view
->UnregisterForVisibleBoundsNotification();
2027 for (int i
= 0; i
< view
->child_count(); ++i
)
2028 UnregisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
2031 void View::RegisterForVisibleBoundsNotification() {
2032 if (registered_for_visible_bounds_notification_
)
2035 registered_for_visible_bounds_notification_
= true;
2036 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
2037 ancestor
->AddDescendantToNotify(this);
2040 void View::UnregisterForVisibleBoundsNotification() {
2041 if (!registered_for_visible_bounds_notification_
)
2044 registered_for_visible_bounds_notification_
= false;
2045 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
2046 ancestor
->RemoveDescendantToNotify(this);
2049 void View::AddDescendantToNotify(View
* view
) {
2051 if (!descendants_to_notify_
.get())
2052 descendants_to_notify_
.reset(new Views
);
2053 descendants_to_notify_
->push_back(view
);
2056 void View::RemoveDescendantToNotify(View
* view
) {
2057 DCHECK(view
&& descendants_to_notify_
.get());
2058 Views::iterator
i(std::find(
2059 descendants_to_notify_
->begin(), descendants_to_notify_
->end(), view
));
2060 DCHECK(i
!= descendants_to_notify_
->end());
2061 descendants_to_notify_
->erase(i
);
2062 if (descendants_to_notify_
->empty())
2063 descendants_to_notify_
.reset();
2066 void View::SetLayerBounds(const gfx::Rect
& bounds
) {
2067 layer()->SetBounds(bounds
);
2070 void View::SetRootBoundsDirty(bool origin_changed
) {
2071 root_bounds_dirty_
= true;
2073 if (origin_changed
) {
2074 // Inform our children that their root bounds are dirty, as their relative
2075 // coordinates in paint root space have changed since ours have changed.
2076 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end();
2078 if (!(*i
)->IsPaintRoot())
2079 (*i
)->SetRootBoundsDirty(origin_changed
);
2084 void View::UpdateRootBounds(gfx::RTree
* tree
, const gfx::Vector2d
& offset
) {
2085 // No need to recompute bounds if we haven't flagged ours as dirty.
2086 TRACE_EVENT1("views", "View::UpdateRootBounds", "class", GetClassName());
2088 // Add our own offset to the provided offset, for our own bounds update and
2089 // for propagation to our children if needed.
2090 gfx::Vector2d view_offset
= offset
+ GetMirroredBounds().OffsetFromOrigin();
2092 // If our bounds have changed we must re-insert our new bounds to the tree.
2093 if (root_bounds_dirty_
) {
2094 root_bounds_dirty_
= false;
2096 view_offset
.x(), view_offset
.y(), bounds_
.width(), bounds_
.height());
2097 tree
->Insert(bounds
, reinterpret_cast<intptr_t>(this));
2100 // Update our children's bounds if needed.
2101 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2102 // We don't descend in to layer views for bounds recomputation, as they
2103 // manage their own RTree as paint roots.
2104 if (!(*i
)->IsPaintRoot())
2105 (*i
)->UpdateRootBounds(tree
, view_offset
);
2109 void View::RemoveRootBounds(gfx::RTree
* tree
) {
2110 tree
->Remove(reinterpret_cast<intptr_t>(this));
2112 root_bounds_dirty_
= true;
2114 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2115 if (!(*i
)->IsPaintRoot())
2116 (*i
)->RemoveRootBounds(tree
);
2120 gfx::RTree
* View::GetBoundsTreeFromPaintRoot() {
2121 gfx::RTree
* bounds_tree
= bounds_tree_
.get();
2122 View
* paint_root
= this;
2123 while (!bounds_tree
&& !paint_root
->IsPaintRoot()) {
2124 // Assumption is that if IsPaintRoot() is false then parent_ is valid.
2126 paint_root
= paint_root
->parent_
;
2127 bounds_tree
= paint_root
->bounds_tree_
.get();
2133 // Transformations -------------------------------------------------------------
2135 bool View::GetTransformRelativeTo(const View
* ancestor
,
2136 gfx::Transform
* transform
) const {
2137 const View
* p
= this;
2139 while (p
&& p
!= ancestor
) {
2140 transform
->ConcatTransform(p
->GetTransform());
2141 gfx::Transform translation
;
2142 translation
.Translate(static_cast<float>(p
->GetMirroredX()),
2143 static_cast<float>(p
->y()));
2144 transform
->ConcatTransform(translation
);
2149 return p
== ancestor
;
2152 // Coordinate conversion -------------------------------------------------------
2154 bool View::ConvertPointForAncestor(const View
* ancestor
,
2155 gfx::Point
* point
) const {
2156 gfx::Transform trans
;
2157 // TODO(sad): Have some way of caching the transformation results.
2158 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2159 gfx::Point3F
p(*point
);
2160 trans
.TransformPoint(&p
);
2161 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2165 bool View::ConvertPointFromAncestor(const View
* ancestor
,
2166 gfx::Point
* point
) const {
2167 gfx::Transform trans
;
2168 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2169 gfx::Point3F
p(*point
);
2170 trans
.TransformPointReverse(&p
);
2171 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2175 bool View::ConvertRectForAncestor(const View
* ancestor
,
2176 gfx::RectF
* rect
) const {
2177 gfx::Transform trans
;
2178 // TODO(sad): Have some way of caching the transformation results.
2179 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2180 trans
.TransformRect(rect
);
2184 bool View::ConvertRectFromAncestor(const View
* ancestor
,
2185 gfx::RectF
* rect
) const {
2186 gfx::Transform trans
;
2187 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2188 trans
.TransformRectReverse(rect
);
2192 // Accelerated painting --------------------------------------------------------
2194 void View::CreateLayer() {
2195 // A new layer is being created for the view. So all the layers of the
2196 // sub-tree can inherit the visibility of the corresponding view.
2197 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2198 child_at(i
)->UpdateChildLayerVisibility(true);
2200 SetLayer(new ui::Layer());
2201 layer()->set_delegate(this);
2202 #if !defined(NDEBUG)
2203 layer()->set_name(GetClassName());
2206 UpdateParentLayers();
2207 UpdateLayerVisibility();
2209 // The new layer needs to be ordered in the layer tree according
2210 // to the view tree. Children of this layer were added in order
2211 // in UpdateParentLayers().
2213 parent()->ReorderLayers();
2215 Widget
* widget
= GetWidget();
2217 widget
->UpdateRootLayers();
2220 void View::UpdateParentLayers() {
2221 // Attach all top-level un-parented layers.
2222 if (layer() && !layer()->parent()) {
2223 UpdateParentLayer();
2225 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2226 child_at(i
)->UpdateParentLayers();
2230 void View::OrphanLayers() {
2232 if (layer()->parent())
2233 layer()->parent()->Remove(layer());
2235 // The layer belonging to this View has already been orphaned. It is not
2236 // necessary to orphan the child layers.
2239 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2240 child_at(i
)->OrphanLayers();
2243 void View::ReparentLayer(const gfx::Vector2d
& offset
, ui::Layer
* parent_layer
) {
2244 layer()->SetBounds(GetLocalBounds() + offset
);
2245 DCHECK_NE(layer(), parent_layer
);
2247 parent_layer
->Add(layer());
2248 layer()->SchedulePaint(GetLocalBounds());
2249 MoveLayerToParent(layer(), gfx::Point());
2252 void View::DestroyLayer() {
2253 ui::Layer
* new_parent
= layer()->parent();
2254 std::vector
<ui::Layer
*> children
= layer()->children();
2255 for (size_t i
= 0; i
< children
.size(); ++i
) {
2256 layer()->Remove(children
[i
]);
2258 new_parent
->Add(children
[i
]);
2261 LayerOwner::DestroyLayer();
2266 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
2270 Widget
* widget
= GetWidget();
2272 widget
->UpdateRootLayers();
2275 // Input -----------------------------------------------------------------------
2277 bool View::ProcessMousePressed(const ui::MouseEvent
& event
) {
2278 int drag_operations
=
2279 (enabled_
&& event
.IsOnlyLeftMouseButton() &&
2280 HitTestPoint(event
.location())) ?
2281 GetDragOperations(event
.location()) : 0;
2282 ContextMenuController
* context_menu_controller
= event
.IsRightMouseButton() ?
2283 context_menu_controller_
: 0;
2284 View::DragInfo
* drag_info
= GetDragInfo();
2286 // TODO(sky): for debugging 360238.
2288 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2289 kContextMenuOnMousePress
&& HitTestPoint(event
.location())) {
2290 ViewStorage
* view_storage
= ViewStorage::GetInstance();
2291 storage_id
= view_storage
->CreateStorageID();
2292 view_storage
->StoreView(storage_id
, this);
2295 const bool enabled
= enabled_
;
2296 const bool result
= OnMousePressed(event
);
2301 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2302 kContextMenuOnMousePress
) {
2303 // Assume that if there is a context menu controller we won't be deleted
2304 // from mouse pressed.
2305 gfx::Point
location(event
.location());
2306 if (HitTestPoint(location
)) {
2307 if (storage_id
!= 0)
2308 CHECK_EQ(this, ViewStorage::GetInstance()->RetrieveView(storage_id
));
2309 ConvertPointToScreen(this, &location
);
2310 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2315 // WARNING: we may have been deleted, don't use any View variables.
2316 if (drag_operations
!= ui::DragDropTypes::DRAG_NONE
) {
2317 drag_info
->PossibleDrag(event
.location());
2320 return !!context_menu_controller
|| result
;
2323 bool View::ProcessMouseDragged(const ui::MouseEvent
& event
) {
2324 // Copy the field, that way if we're deleted after drag and drop no harm is
2326 ContextMenuController
* context_menu_controller
= context_menu_controller_
;
2327 const bool possible_drag
= GetDragInfo()->possible_drag
;
2328 if (possible_drag
&&
2329 ExceededDragThreshold(GetDragInfo()->start_pt
- event
.location()) &&
2330 (!drag_controller_
||
2331 drag_controller_
->CanStartDragForView(
2332 this, GetDragInfo()->start_pt
, event
.location()))) {
2333 DoDrag(event
, GetDragInfo()->start_pt
,
2334 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
);
2336 if (OnMouseDragged(event
))
2338 // Fall through to return value based on context menu controller.
2340 // WARNING: we may have been deleted.
2341 return (context_menu_controller
!= NULL
) || possible_drag
;
2344 void View::ProcessMouseReleased(const ui::MouseEvent
& event
) {
2345 if (!kContextMenuOnMousePress
&& context_menu_controller_
&&
2346 event
.IsOnlyRightMouseButton()) {
2347 // Assume that if there is a context menu controller we won't be deleted
2348 // from mouse released.
2349 gfx::Point
location(event
.location());
2350 OnMouseReleased(event
);
2351 if (HitTestPoint(location
)) {
2352 ConvertPointToScreen(this, &location
);
2353 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2356 OnMouseReleased(event
);
2358 // WARNING: we may have been deleted.
2361 // Accelerators ----------------------------------------------------------------
2363 void View::RegisterPendingAccelerators() {
2364 if (!accelerators_
.get() ||
2365 registered_accelerator_count_
== accelerators_
->size()) {
2366 // No accelerators are waiting for registration.
2371 // The view is not yet attached to a widget, defer registration until then.
2375 accelerator_focus_manager_
= GetFocusManager();
2376 if (!accelerator_focus_manager_
) {
2377 // Some crash reports seem to show that we may get cases where we have no
2378 // focus manager (see bug #1291225). This should never be the case, just
2379 // making sure we don't crash.
2383 for (std::vector
<ui::Accelerator
>::const_iterator
i(
2384 accelerators_
->begin() + registered_accelerator_count_
);
2385 i
!= accelerators_
->end(); ++i
) {
2386 accelerator_focus_manager_
->RegisterAccelerator(
2387 *i
, ui::AcceleratorManager::kNormalPriority
, this);
2389 registered_accelerator_count_
= accelerators_
->size();
2392 void View::UnregisterAccelerators(bool leave_data_intact
) {
2393 if (!accelerators_
.get())
2397 if (accelerator_focus_manager_
) {
2398 accelerator_focus_manager_
->UnregisterAccelerators(this);
2399 accelerator_focus_manager_
= NULL
;
2401 if (!leave_data_intact
) {
2402 accelerators_
->clear();
2403 accelerators_
.reset();
2405 registered_accelerator_count_
= 0;
2409 // Focus -----------------------------------------------------------------------
2411 void View::InitFocusSiblings(View
* v
, int index
) {
2412 int count
= child_count();
2415 v
->next_focusable_view_
= NULL
;
2416 v
->previous_focusable_view_
= NULL
;
2418 if (index
== count
) {
2419 // We are inserting at the end, but the end of the child list may not be
2420 // the last focusable element. Let's try to find an element with no next
2421 // focusable element to link to.
2422 View
* last_focusable_view
= NULL
;
2423 for (Views::iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2424 if (!(*i
)->next_focusable_view_
) {
2425 last_focusable_view
= *i
;
2429 if (last_focusable_view
== NULL
) {
2430 // Hum... there is a cycle in the focus list. Let's just insert ourself
2431 // after the last child.
2432 View
* prev
= children_
[index
- 1];
2433 v
->previous_focusable_view_
= prev
;
2434 v
->next_focusable_view_
= prev
->next_focusable_view_
;
2435 prev
->next_focusable_view_
->previous_focusable_view_
= v
;
2436 prev
->next_focusable_view_
= v
;
2438 last_focusable_view
->next_focusable_view_
= v
;
2439 v
->next_focusable_view_
= NULL
;
2440 v
->previous_focusable_view_
= last_focusable_view
;
2443 View
* prev
= children_
[index
]->GetPreviousFocusableView();
2444 v
->previous_focusable_view_
= prev
;
2445 v
->next_focusable_view_
= children_
[index
];
2447 prev
->next_focusable_view_
= v
;
2448 children_
[index
]->previous_focusable_view_
= v
;
2453 // System events ---------------------------------------------------------------
2455 void View::PropagateThemeChanged() {
2456 for (int i
= child_count() - 1; i
>= 0; --i
)
2457 child_at(i
)->PropagateThemeChanged();
2461 void View::PropagateLocaleChanged() {
2462 for (int i
= child_count() - 1; i
>= 0; --i
)
2463 child_at(i
)->PropagateLocaleChanged();
2467 // Tooltips --------------------------------------------------------------------
2469 void View::UpdateTooltip() {
2470 Widget
* widget
= GetWidget();
2471 // TODO(beng): The TooltipManager NULL check can be removed when we
2472 // consolidate Init() methods and make views_unittests Init() all
2473 // Widgets that it uses.
2474 if (widget
&& widget
->GetTooltipManager())
2475 widget
->GetTooltipManager()->UpdateTooltip();
2478 // Drag and drop ---------------------------------------------------------------
2480 bool View::DoDrag(const ui::LocatedEvent
& event
,
2481 const gfx::Point
& press_pt
,
2482 ui::DragDropTypes::DragEventSource source
) {
2483 int drag_operations
= GetDragOperations(press_pt
);
2484 if (drag_operations
== ui::DragDropTypes::DRAG_NONE
)
2487 Widget
* widget
= GetWidget();
2488 // We should only start a drag from an event, implying we have a widget.
2491 // Don't attempt to start a drag while in the process of dragging. This is
2492 // especially important on X where we can get multiple mouse move events when
2493 // we start the drag.
2494 if (widget
->dragged_view())
2497 OSExchangeData data
;
2498 WriteDragData(press_pt
, &data
);
2500 // Message the RootView to do the drag and drop. That way if we're removed
2501 // the RootView can detect it and avoid calling us back.
2502 gfx::Point
widget_location(event
.location());
2503 ConvertPointToWidget(this, &widget_location
);
2504 widget
->RunShellDrag(this, data
, widget_location
, drag_operations
, source
);
2505 // WARNING: we may have been deleted.
2509 } // namespace views