1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first.
7 #include "ui/views/view.h"
12 #include "base/debug/trace_event.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "third_party/skia/include/core/SkRect.h"
19 #include "ui/accessibility/ax_enums.h"
20 #include "ui/base/cursor/cursor.h"
21 #include "ui/base/dragdrop/drag_drop_types.h"
22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/dip_util.h"
24 #include "ui/compositor/layer.h"
25 #include "ui/compositor/layer_animator.h"
26 #include "ui/events/event_target_iterator.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/interpolated_transform.h"
29 #include "ui/gfx/path.h"
30 #include "ui/gfx/point3_f.h"
31 #include "ui/gfx/point_conversions.h"
32 #include "ui/gfx/rect_conversions.h"
33 #include "ui/gfx/scoped_canvas.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/skia_util.h"
36 #include "ui/gfx/transform.h"
37 #include "ui/native_theme/native_theme.h"
38 #include "ui/views/accessibility/native_view_accessibility.h"
39 #include "ui/views/background.h"
40 #include "ui/views/border.h"
41 #include "ui/views/context_menu_controller.h"
42 #include "ui/views/drag_controller.h"
43 #include "ui/views/focus/view_storage.h"
44 #include "ui/views/layout/layout_manager.h"
45 #include "ui/views/rect_based_targeting_utils.h"
46 #include "ui/views/views_delegate.h"
47 #include "ui/views/widget/native_widget_private.h"
48 #include "ui/views/widget/root_view.h"
49 #include "ui/views/widget/tooltip_manager.h"
50 #include "ui/views/widget/widget.h"
53 #include "base/win/scoped_gdi_object.h"
59 const bool kContextMenuOnMousePress
= false;
61 const bool kContextMenuOnMousePress
= true;
64 // The minimum percentage of a view's area that needs to be covered by a rect
65 // representing a touch region in order for that view to be considered by the
66 // rect-based targeting algorithm.
67 static const float kRectTargetOverlap
= 0.6f
;
69 // Default horizontal drag threshold in pixels.
70 // Same as what gtk uses.
71 const int kDefaultHorizontalDragThreshold
= 8;
73 // Default vertical drag threshold in pixels.
74 // Same as what gtk uses.
75 const int kDefaultVerticalDragThreshold
= 8;
77 // Returns the top view in |view|'s hierarchy.
78 const views::View
* GetHierarchyRoot(const views::View
* view
) {
79 const views::View
* root
= view
;
80 while (root
&& root
->parent())
81 root
= root
->parent();
91 } // namespace internal
94 ViewsDelegate
* ViewsDelegate::views_delegate
= NULL
;
97 const char View::kViewClassName
[] = "View";
99 ////////////////////////////////////////////////////////////////////////////////
102 // Creation and lifetime -------------------------------------------------------
105 : owned_by_client_(false),
111 notify_enter_exit_on_child_(false),
112 registered_for_visible_bounds_notification_(false),
113 root_bounds_dirty_(true),
114 clip_insets_(0, 0, 0, 0),
116 snap_layer_to_pixel_boundary_(false),
117 flip_canvas_on_paint_for_rtl_ui_(false),
118 paint_to_layer_(false),
119 accelerator_focus_manager_(NULL
),
120 registered_accelerator_count_(0),
121 next_focusable_view_(NULL
),
122 previous_focusable_view_(NULL
),
124 accessibility_focusable_(false),
125 context_menu_controller_(NULL
),
126 drag_controller_(NULL
),
127 native_view_accessibility_(NULL
) {
132 parent_
->RemoveChildView(this);
134 ViewStorage::GetInstance()->ViewRemoved(this);
136 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
137 (*i
)->parent_
= NULL
;
138 if (!(*i
)->owned_by_client_
)
142 // Release ownership of the native accessibility object, but it's
143 // reference-counted on some platforms, so it may not be deleted right away.
144 if (native_view_accessibility_
)
145 native_view_accessibility_
->Destroy();
148 // Tree operations -------------------------------------------------------------
150 const Widget
* View::GetWidget() const {
151 // The root view holds a reference to this view hierarchy's Widget.
152 return parent_
? parent_
->GetWidget() : NULL
;
155 Widget
* View::GetWidget() {
156 return const_cast<Widget
*>(const_cast<const View
*>(this)->GetWidget());
159 void View::AddChildView(View
* view
) {
160 if (view
->parent_
== this)
162 AddChildViewAt(view
, child_count());
165 void View::AddChildViewAt(View
* view
, int index
) {
166 CHECK_NE(view
, this) << "You cannot add a view as its own child";
168 DCHECK_LE(index
, child_count());
170 // If |view| has a parent, remove it from its parent.
171 View
* parent
= view
->parent_
;
172 ui::NativeTheme
* old_theme
= NULL
;
174 old_theme
= view
->GetNativeTheme();
175 if (parent
== this) {
176 ReorderChildView(view
, index
);
179 parent
->DoRemoveChildView(view
, true, true, false, this);
182 // Sets the prev/next focus views.
183 InitFocusSiblings(view
, index
);
185 // Let's insert the view.
186 view
->parent_
= this;
187 children_
.insert(children_
.begin() + index
, view
);
189 // Instruct the view to recompute its root bounds on next Paint().
190 view
->SetRootBoundsDirty(true);
192 views::Widget
* widget
= GetWidget();
194 const ui::NativeTheme
* new_theme
= view
->GetNativeTheme();
195 if (new_theme
!= old_theme
)
196 view
->PropagateNativeThemeChanged(new_theme
);
199 ViewHierarchyChangedDetails
details(true, this, view
, parent
);
201 for (View
* v
= this; v
; v
= v
->parent_
)
202 v
->ViewHierarchyChangedImpl(false, details
);
204 view
->PropagateAddNotifications(details
);
207 RegisterChildrenForVisibleBoundsNotification(view
);
209 view
->SchedulePaint();
212 if (layout_manager_
.get())
213 layout_manager_
->ViewAdded(this, view
);
217 // Make sure the visibility of the child layers are correct.
218 // If any of the parent View is hidden, then the layers of the subtree
219 // rooted at |this| should be hidden. Otherwise, all the child layers should
220 // inherit the visibility of the owner View.
221 UpdateLayerVisibility();
224 void View::ReorderChildView(View
* view
, int index
) {
225 DCHECK_EQ(view
->parent_
, this);
227 index
= child_count() - 1;
228 else if (index
>= child_count())
230 if (children_
[index
] == view
)
233 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
234 DCHECK(i
!= children_
.end());
237 // Unlink the view first
238 View
* next_focusable
= view
->next_focusable_view_
;
239 View
* prev_focusable
= view
->previous_focusable_view_
;
241 prev_focusable
->next_focusable_view_
= next_focusable
;
243 next_focusable
->previous_focusable_view_
= prev_focusable
;
245 // Add it in the specified index now.
246 InitFocusSiblings(view
, index
);
247 children_
.insert(children_
.begin() + index
, view
);
252 void View::RemoveChildView(View
* view
) {
253 DoRemoveChildView(view
, true, true, false, NULL
);
256 void View::RemoveAllChildViews(bool delete_children
) {
257 while (!children_
.empty())
258 DoRemoveChildView(children_
.front(), false, false, delete_children
, NULL
);
262 bool View::Contains(const View
* view
) const {
263 for (const View
* v
= view
; v
; v
= v
->parent_
) {
270 int View::GetIndexOf(const View
* view
) const {
271 Views::const_iterator
i(std::find(children_
.begin(), children_
.end(), view
));
272 return i
!= children_
.end() ? static_cast<int>(i
- children_
.begin()) : -1;
275 // Size and disposition --------------------------------------------------------
277 void View::SetBounds(int x
, int y
, int width
, int height
) {
278 SetBoundsRect(gfx::Rect(x
, y
, std::max(0, width
), std::max(0, height
)));
281 void View::SetBoundsRect(const gfx::Rect
& bounds
) {
282 if (bounds
== bounds_
) {
284 needs_layout_
= false;
291 // Paint where the view is currently.
292 SchedulePaintBoundsChanged(
293 bounds_
.size() == bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
294 SCHEDULE_PAINT_SIZE_CHANGED
);
297 gfx::Rect prev
= bounds_
;
302 void View::SetSize(const gfx::Size
& size
) {
303 SetBounds(x(), y(), size
.width(), size
.height());
306 void View::SetPosition(const gfx::Point
& position
) {
307 SetBounds(position
.x(), position
.y(), width(), height());
310 void View::SetX(int x
) {
311 SetBounds(x
, y(), width(), height());
314 void View::SetY(int y
) {
315 SetBounds(x(), y
, width(), height());
318 gfx::Rect
View::GetContentsBounds() const {
319 gfx::Rect
contents_bounds(GetLocalBounds());
321 contents_bounds
.Inset(border_
->GetInsets());
322 return contents_bounds
;
325 gfx::Rect
View::GetLocalBounds() const {
326 return gfx::Rect(size());
329 gfx::Rect
View::GetLayerBoundsInPixel() const {
330 return layer()->GetTargetBounds();
333 gfx::Insets
View::GetInsets() const {
334 return border_
.get() ? border_
->GetInsets() : gfx::Insets();
337 gfx::Rect
View::GetVisibleBounds() const {
340 gfx::Rect
vis_bounds(GetLocalBounds());
341 gfx::Rect ancestor_bounds
;
342 const View
* view
= this;
343 gfx::Transform transform
;
345 while (view
!= NULL
&& !vis_bounds
.IsEmpty()) {
346 transform
.ConcatTransform(view
->GetTransform());
347 gfx::Transform translation
;
348 translation
.Translate(static_cast<float>(view
->GetMirroredX()),
349 static_cast<float>(view
->y()));
350 transform
.ConcatTransform(translation
);
352 vis_bounds
= view
->ConvertRectToParent(vis_bounds
);
353 const View
* ancestor
= view
->parent_
;
354 if (ancestor
!= NULL
) {
355 ancestor_bounds
.SetRect(0, 0, ancestor
->width(), ancestor
->height());
356 vis_bounds
.Intersect(ancestor_bounds
);
357 } else if (!view
->GetWidget()) {
358 // If the view has no Widget, we're not visible. Return an empty rect.
363 if (vis_bounds
.IsEmpty())
365 // Convert back to this views coordinate system.
366 gfx::RectF
views_vis_bounds(vis_bounds
);
367 transform
.TransformRectReverse(&views_vis_bounds
);
368 // Partially visible pixels should be considered visible.
369 return gfx::ToEnclosingRect(views_vis_bounds
);
372 gfx::Rect
View::GetBoundsInScreen() const {
374 View::ConvertPointToScreen(this, &origin
);
375 return gfx::Rect(origin
, size());
378 gfx::Size
View::GetPreferredSize() const {
379 if (layout_manager_
.get())
380 return layout_manager_
->GetPreferredSize(this);
384 int View::GetBaseline() const {
388 void View::SizeToPreferredSize() {
389 gfx::Size prefsize
= GetPreferredSize();
390 if ((prefsize
.width() != width()) || (prefsize
.height() != height()))
391 SetBounds(x(), y(), prefsize
.width(), prefsize
.height());
394 gfx::Size
View::GetMinimumSize() const {
395 return GetPreferredSize();
398 gfx::Size
View::GetMaximumSize() const {
402 int View::GetHeightForWidth(int w
) const {
403 if (layout_manager_
.get())
404 return layout_manager_
->GetPreferredHeightForWidth(this, w
);
405 return GetPreferredSize().height();
408 void View::SetVisible(bool visible
) {
409 if (visible
!= visible_
) {
410 // If the View is currently visible, schedule paint to refresh parent.
411 // TODO(beng): not sure we should be doing this if we have a layer.
417 // Notify the parent.
419 parent_
->ChildVisibilityChanged(this);
421 // This notifies all sub-views recursively.
422 PropagateVisibilityNotifications(this, visible_
);
423 UpdateLayerVisibility();
425 // If we are newly visible, schedule paint.
431 bool View::IsDrawn() const {
432 return visible_
&& parent_
? parent_
->IsDrawn() : false;
435 void View::SetEnabled(bool enabled
) {
436 if (enabled
!= enabled_
) {
442 void View::OnEnabledChanged() {
446 // Transformations -------------------------------------------------------------
448 gfx::Transform
View::GetTransform() const {
449 return layer() ? layer()->transform() : gfx::Transform();
452 void View::SetTransform(const gfx::Transform
& transform
) {
453 if (transform
.IsIdentity()) {
455 layer()->SetTransform(transform
);
456 if (!paint_to_layer_
)
464 layer()->SetTransform(transform
);
465 layer()->ScheduleDraw();
469 void View::SetPaintToLayer(bool paint_to_layer
) {
470 if (paint_to_layer_
== paint_to_layer
)
473 // If this is a change in state we will also need to update bounds trees.
474 if (paint_to_layer
) {
475 // Gaining a layer means becoming a paint root. We must remove ourselves
476 // from our old paint root, if we had one. Traverse up view tree to find old
478 View
* old_paint_root
= parent_
;
479 while (old_paint_root
&& !old_paint_root
->IsPaintRoot())
480 old_paint_root
= old_paint_root
->parent_
;
482 // Remove our and our children's bounds from the old tree. This will also
483 // mark all of our bounds as dirty.
484 if (old_paint_root
&& old_paint_root
->bounds_tree_
)
485 RemoveRootBounds(old_paint_root
->bounds_tree_
.get());
488 // Losing a layer means we are no longer a paint root, so delete our
489 // bounds tree and mark ourselves as dirty for future insertion into our
490 // new paint root's bounds tree.
491 bounds_tree_
.reset();
492 SetRootBoundsDirty(true);
495 paint_to_layer_
= paint_to_layer
;
496 if (paint_to_layer_
&& !layer()) {
498 } else if (!paint_to_layer_
&& layer()) {
503 // RTL positioning -------------------------------------------------------------
505 gfx::Rect
View::GetMirroredBounds() const {
506 gfx::Rect
bounds(bounds_
);
507 bounds
.set_x(GetMirroredX());
511 gfx::Point
View::GetMirroredPosition() const {
512 return gfx::Point(GetMirroredX(), y());
515 int View::GetMirroredX() const {
516 return parent_
? parent_
->GetMirroredXForRect(bounds_
) : x();
519 int View::GetMirroredXForRect(const gfx::Rect
& bounds
) const {
520 return base::i18n::IsRTL() ?
521 (width() - bounds
.x() - bounds
.width()) : bounds
.x();
524 int View::GetMirroredXInView(int x
) const {
525 return base::i18n::IsRTL() ? width() - x
: x
;
528 int View::GetMirroredXWithWidthInView(int x
, int w
) const {
529 return base::i18n::IsRTL() ? width() - x
- w
: x
;
532 // Layout ----------------------------------------------------------------------
534 void View::Layout() {
535 needs_layout_
= false;
537 // If we have a layout manager, let it handle the layout for us.
538 if (layout_manager_
.get())
539 layout_manager_
->Layout(this);
541 // Make sure to propagate the Layout() call to any children that haven't
542 // received it yet through the layout manager and need to be laid out. This
543 // is needed for the case when the child requires a layout but its bounds
544 // weren't changed by the layout manager. If there is no layout manager, we
545 // just propagate the Layout() call down the hierarchy, so whoever receives
546 // the call can take appropriate action.
547 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
548 View
* child
= child_at(i
);
549 if (child
->needs_layout_
|| !layout_manager_
.get()) {
550 child
->needs_layout_
= false;
556 void View::InvalidateLayout() {
557 // Always invalidate up. This is needed to handle the case of us already being
558 // valid, but not our parent.
559 needs_layout_
= true;
561 parent_
->InvalidateLayout();
564 LayoutManager
* View::GetLayoutManager() const {
565 return layout_manager_
.get();
568 void View::SetLayoutManager(LayoutManager
* layout_manager
) {
569 if (layout_manager_
.get())
570 layout_manager_
->Uninstalled(this);
572 layout_manager_
.reset(layout_manager
);
573 if (layout_manager_
.get())
574 layout_manager_
->Installed(this);
577 void View::SnapLayerToPixelBoundary() {
581 if (snap_layer_to_pixel_boundary_
&& layer()->parent() &&
582 layer()->GetCompositor()) {
583 ui::SnapLayerToPhysicalPixelBoundary(layer()->parent(), layer());
586 layer()->SetSubpixelPositionOffset(gfx::Vector2dF());
590 // Attributes ------------------------------------------------------------------
592 const char* View::GetClassName() const {
593 return kViewClassName
;
596 const View
* View::GetAncestorWithClassName(const std::string
& name
) const {
597 for (const View
* view
= this; view
; view
= view
->parent_
) {
598 if (!strcmp(view
->GetClassName(), name
.c_str()))
604 View
* View::GetAncestorWithClassName(const std::string
& name
) {
605 return const_cast<View
*>(const_cast<const View
*>(this)->
606 GetAncestorWithClassName(name
));
609 const View
* View::GetViewByID(int id
) const {
611 return const_cast<View
*>(this);
613 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
614 const View
* view
= child_at(i
)->GetViewByID(id
);
621 View
* View::GetViewByID(int id
) {
622 return const_cast<View
*>(const_cast<const View
*>(this)->GetViewByID(id
));
625 void View::SetGroup(int gid
) {
626 // Don't change the group id once it's set.
627 DCHECK(group_
== -1 || group_
== gid
);
631 int View::GetGroup() const {
635 bool View::IsGroupFocusTraversable() const {
639 void View::GetViewsInGroup(int group
, Views
* views
) {
641 views
->push_back(this);
643 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
644 child_at(i
)->GetViewsInGroup(group
, views
);
647 View
* View::GetSelectedViewForGroup(int group
) {
649 GetWidget()->GetRootView()->GetViewsInGroup(group
, &views
);
650 return views
.empty() ? NULL
: views
[0];
653 // Coordinate conversion -------------------------------------------------------
656 void View::ConvertPointToTarget(const View
* source
,
661 if (source
== target
)
664 const View
* root
= GetHierarchyRoot(target
);
665 CHECK_EQ(GetHierarchyRoot(source
), root
);
668 source
->ConvertPointForAncestor(root
, point
);
671 target
->ConvertPointFromAncestor(root
, point
);
675 void View::ConvertRectToTarget(const View
* source
,
680 if (source
== target
)
683 const View
* root
= GetHierarchyRoot(target
);
684 CHECK_EQ(GetHierarchyRoot(source
), root
);
687 source
->ConvertRectForAncestor(root
, rect
);
690 target
->ConvertRectFromAncestor(root
, rect
);
694 void View::ConvertPointToWidget(const View
* src
, gfx::Point
* p
) {
698 src
->ConvertPointForAncestor(NULL
, p
);
702 void View::ConvertPointFromWidget(const View
* dest
, gfx::Point
* p
) {
706 dest
->ConvertPointFromAncestor(NULL
, p
);
710 void View::ConvertPointToScreen(const View
* src
, gfx::Point
* p
) {
714 // If the view is not connected to a tree, there's nothing we can do.
715 const Widget
* widget
= src
->GetWidget();
717 ConvertPointToWidget(src
, p
);
718 *p
+= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
723 void View::ConvertPointFromScreen(const View
* dst
, gfx::Point
* p
) {
727 const views::Widget
* widget
= dst
->GetWidget();
730 *p
-= widget
->GetClientAreaBoundsInScreen().OffsetFromOrigin();
731 views::View::ConvertPointFromWidget(dst
, p
);
734 gfx::Rect
View::ConvertRectToParent(const gfx::Rect
& rect
) const {
735 gfx::RectF x_rect
= rect
;
736 GetTransform().TransformRect(&x_rect
);
737 x_rect
.Offset(GetMirroredPosition().OffsetFromOrigin());
738 // Pixels we partially occupy in the parent should be included.
739 return gfx::ToEnclosingRect(x_rect
);
742 gfx::Rect
View::ConvertRectToWidget(const gfx::Rect
& rect
) const {
743 gfx::Rect x_rect
= rect
;
744 for (const View
* v
= this; v
; v
= v
->parent_
)
745 x_rect
= v
->ConvertRectToParent(x_rect
);
749 // Painting --------------------------------------------------------------------
751 void View::SchedulePaint() {
752 SchedulePaintInRect(GetLocalBounds());
755 void View::SchedulePaintInRect(const gfx::Rect
& rect
) {
760 layer()->SchedulePaint(rect
);
761 } else if (parent_
) {
762 // Translate the requested paint rect to the parent's coordinate system
763 // then pass this notification up to the parent.
764 parent_
->SchedulePaintInRect(ConvertRectToParent(rect
));
768 void View::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
769 // The cull_set may allow us to skip painting without canvas construction or
770 // even canvas rect intersection.
771 if (cull_set
.ShouldPaint(this)) {
772 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
774 gfx::ScopedCanvas
scoped_canvas(canvas
);
776 // Paint this View and its children, setting the clip rect to the bounds
777 // of this View and translating the origin to the local bounds' top left
780 // Note that the X (or left) position we pass to ClipRectInt takes into
781 // consideration whether or not the view uses a right-to-left layout so that
782 // we paint our view in its mirrored position if need be.
783 gfx::Rect clip_rect
= bounds();
784 clip_rect
.Inset(clip_insets_
);
786 clip_rect
.set_x(parent_
->GetMirroredXForRect(clip_rect
));
787 canvas
->ClipRect(clip_rect
);
788 if (canvas
->IsClipEmpty())
791 // Non-empty clip, translate the graphics such that 0,0 corresponds to where
792 // this view is located (related to its parent).
793 canvas
->Translate(GetMirroredPosition().OffsetFromOrigin());
794 canvas
->Transform(GetTransform());
796 // If we are a paint root, we need to construct our own CullSet object for
797 // propagation to our children.
800 bounds_tree_
.reset(new BoundsTree(2, 5));
802 // Recompute our bounds tree as needed.
803 UpdateRootBounds(bounds_tree_
.get(), gfx::Vector2d());
805 // Grab the clip rect from the supplied canvas to use as the query rect.
806 gfx::Rect canvas_bounds
;
807 if (!canvas
->GetClipBounds(&canvas_bounds
)) {
808 NOTREACHED() << "Failed to get clip bounds from the canvas!";
812 // Now query our bounds_tree_ for a set of damaged views that intersect
813 // our canvas bounds.
814 scoped_ptr
<base::hash_set
<intptr_t> > damaged_views(
815 new base::hash_set
<intptr_t>());
816 bounds_tree_
->AppendIntersectingRecords(
817 canvas_bounds
, damaged_views
.get());
818 // Construct a CullSet to wrap the damaged views set, it will delete it
819 // for us on scope exit.
820 CullSet
paint_root_cull_set(damaged_views
.Pass());
821 // Paint all descendents using our new cull set.
822 PaintCommon(canvas
, paint_root_cull_set
);
824 // Not a paint root, so we can proceed as normal.
825 PaintCommon(canvas
, cull_set
);
830 void View::set_background(Background
* b
) {
831 background_
.reset(b
);
834 void View::SetBorder(scoped_ptr
<Border
> b
) { border_
= b
.Pass(); }
836 ui::ThemeProvider
* View::GetThemeProvider() const {
837 const Widget
* widget
= GetWidget();
838 return widget
? widget
->GetThemeProvider() : NULL
;
841 const ui::NativeTheme
* View::GetNativeTheme() const {
842 const Widget
* widget
= GetWidget();
843 return widget
? widget
->GetNativeTheme() : ui::NativeTheme::instance();
846 // Input -----------------------------------------------------------------------
848 View
* View::GetEventHandlerForPoint(const gfx::Point
& point
) {
849 return GetEventHandlerForRect(gfx::Rect(point
, gfx::Size(1, 1)));
852 View
* View::GetEventHandlerForRect(const gfx::Rect
& rect
) {
853 // |rect_view| represents the current best candidate to return
854 // if rect-based targeting (i.e., fuzzing) is used.
855 // |rect_view_distance| is used to keep track of the distance
856 // between the center point of |rect_view| and the center
858 View
* rect_view
= NULL
;
859 int rect_view_distance
= INT_MAX
;
861 // |point_view| represents the view that would have been returned
862 // from this function call if point-based targeting were used.
863 View
* point_view
= NULL
;
865 for (int i
= child_count() - 1; i
>= 0; --i
) {
866 View
* child
= child_at(i
);
868 if (!child
->CanProcessEventsWithinSubtree())
871 // Ignore any children which are invisible or do not intersect |rect|.
872 if (!child
->visible())
874 gfx::RectF
rect_in_child_coords_f(rect
);
875 ConvertRectToTarget(this, child
, &rect_in_child_coords_f
);
876 gfx::Rect rect_in_child_coords
= gfx::ToEnclosingRect(
877 rect_in_child_coords_f
);
878 if (!child
->HitTestRect(rect_in_child_coords
))
881 View
* cur_view
= child
->GetEventHandlerForRect(rect_in_child_coords
);
883 if (views::UsePointBasedTargeting(rect
))
886 gfx::RectF
cur_view_bounds_f(cur_view
->GetLocalBounds());
887 ConvertRectToTarget(cur_view
, this, &cur_view_bounds_f
);
888 gfx::Rect cur_view_bounds
= gfx::ToEnclosingRect(
890 if (views::PercentCoveredBy(cur_view_bounds
, rect
) >= kRectTargetOverlap
) {
891 // |cur_view| is a suitable candidate for rect-based targeting.
892 // Check to see if it is the closest suitable candidate so far.
893 gfx::Point
touch_center(rect
.CenterPoint());
894 int cur_dist
= views::DistanceSquaredFromCenterToPoint(touch_center
,
896 if (!rect_view
|| cur_dist
< rect_view_distance
) {
897 rect_view
= cur_view
;
898 rect_view_distance
= cur_dist
;
900 } else if (!rect_view
&& !point_view
) {
901 // Rect-based targeting has not yielded any candidates so far. Check
902 // if point-based targeting would have selected |cur_view|.
903 gfx::Point
point_in_child_coords(rect_in_child_coords
.CenterPoint());
904 if (child
->HitTestPoint(point_in_child_coords
))
905 point_view
= child
->GetEventHandlerForPoint(point_in_child_coords
);
909 if (views::UsePointBasedTargeting(rect
) || (!rect_view
&& !point_view
))
912 // If |this| is a suitable candidate for rect-based targeting, check to
913 // see if it is closer than the current best suitable candidate so far.
914 gfx::Rect
local_bounds(GetLocalBounds());
915 if (views::PercentCoveredBy(local_bounds
, rect
) >= kRectTargetOverlap
) {
916 gfx::Point
touch_center(rect
.CenterPoint());
917 int cur_dist
= views::DistanceSquaredFromCenterToPoint(touch_center
,
919 if (!rect_view
|| cur_dist
< rect_view_distance
)
923 return rect_view
? rect_view
: point_view
;
926 bool View::CanProcessEventsWithinSubtree() const {
930 View
* View::GetTooltipHandlerForPoint(const gfx::Point
& point
) {
931 if (!HitTestPoint(point
) || !CanProcessEventsWithinSubtree())
934 // Walk the child Views recursively looking for the View that most
935 // tightly encloses the specified point.
936 for (int i
= child_count() - 1; i
>= 0; --i
) {
937 View
* child
= child_at(i
);
938 if (!child
->visible())
941 gfx::Point
point_in_child_coords(point
);
942 ConvertPointToTarget(this, child
, &point_in_child_coords
);
943 View
* handler
= child
->GetTooltipHandlerForPoint(point_in_child_coords
);
950 gfx::NativeCursor
View::GetCursor(const ui::MouseEvent
& event
) {
952 static ui::Cursor arrow
;
953 if (!arrow
.platform())
954 arrow
.SetPlatformCursor(LoadCursor(NULL
, IDC_ARROW
));
957 return gfx::kNullCursor
;
961 bool View::HitTestPoint(const gfx::Point
& point
) const {
962 return HitTestRect(gfx::Rect(point
, gfx::Size(1, 1)));
965 bool View::HitTestRect(const gfx::Rect
& rect
) const {
966 ViewTargeter
* view_targeter
= targeter();
968 view_targeter
= GetWidget()->GetRootView()->targeter();
969 CHECK(view_targeter
);
970 return view_targeter
->DoesIntersectRect(this, rect
);
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
<ViewTargeter
>
1107 View::SetEventTargeter(scoped_ptr
<ViewTargeter
> targeter
) {
1108 scoped_ptr
<ViewTargeter
> 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 void View::ConvertEventToTarget(ui::EventTarget
* target
,
1131 ui::LocatedEvent
* event
) {
1132 event
->ConvertLocationToTarget(this, static_cast<View
*>(target
));
1135 // Accelerators ----------------------------------------------------------------
1137 void View::AddAccelerator(const ui::Accelerator
& accelerator
) {
1138 if (!accelerators_
.get())
1139 accelerators_
.reset(new std::vector
<ui::Accelerator
>());
1141 if (std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
) ==
1142 accelerators_
->end()) {
1143 accelerators_
->push_back(accelerator
);
1145 RegisterPendingAccelerators();
1148 void View::RemoveAccelerator(const ui::Accelerator
& accelerator
) {
1149 if (!accelerators_
.get()) {
1150 NOTREACHED() << "Removing non-existing accelerator";
1154 std::vector
<ui::Accelerator
>::iterator
i(
1155 std::find(accelerators_
->begin(), accelerators_
->end(), accelerator
));
1156 if (i
== accelerators_
->end()) {
1157 NOTREACHED() << "Removing non-existing accelerator";
1161 size_t index
= i
- accelerators_
->begin();
1162 accelerators_
->erase(i
);
1163 if (index
>= registered_accelerator_count_
) {
1164 // The accelerator is not registered to FocusManager.
1167 --registered_accelerator_count_
;
1169 // Providing we are attached to a Widget and registered with a focus manager,
1170 // we should de-register from that focus manager now.
1171 if (GetWidget() && accelerator_focus_manager_
)
1172 accelerator_focus_manager_
->UnregisterAccelerator(accelerator
, this);
1175 void View::ResetAccelerators() {
1176 if (accelerators_
.get())
1177 UnregisterAccelerators(false);
1180 bool View::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1184 bool View::CanHandleAccelerators() const {
1185 return enabled() && IsDrawn() && GetWidget() && GetWidget()->IsVisible();
1188 // Focus -----------------------------------------------------------------------
1190 bool View::HasFocus() const {
1191 const FocusManager
* focus_manager
= GetFocusManager();
1192 return focus_manager
&& (focus_manager
->GetFocusedView() == this);
1195 View
* View::GetNextFocusableView() {
1196 return next_focusable_view_
;
1199 const View
* View::GetNextFocusableView() const {
1200 return next_focusable_view_
;
1203 View
* View::GetPreviousFocusableView() {
1204 return previous_focusable_view_
;
1207 void View::SetNextFocusableView(View
* view
) {
1209 view
->previous_focusable_view_
= this;
1210 next_focusable_view_
= view
;
1213 void View::SetFocusable(bool focusable
) {
1214 if (focusable_
== focusable
)
1217 focusable_
= focusable
;
1220 bool View::IsFocusable() const {
1221 return focusable_
&& enabled_
&& IsDrawn();
1224 bool View::IsAccessibilityFocusable() const {
1225 return (focusable_
|| accessibility_focusable_
) && enabled_
&& IsDrawn();
1228 void View::SetAccessibilityFocusable(bool accessibility_focusable
) {
1229 if (accessibility_focusable_
== accessibility_focusable
)
1232 accessibility_focusable_
= accessibility_focusable
;
1235 FocusManager
* View::GetFocusManager() {
1236 Widget
* widget
= GetWidget();
1237 return widget
? widget
->GetFocusManager() : NULL
;
1240 const FocusManager
* View::GetFocusManager() const {
1241 const Widget
* widget
= GetWidget();
1242 return widget
? widget
->GetFocusManager() : NULL
;
1245 void View::RequestFocus() {
1246 FocusManager
* focus_manager
= GetFocusManager();
1247 if (focus_manager
&& IsFocusable())
1248 focus_manager
->SetFocusedView(this);
1251 bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent
& event
) {
1255 FocusTraversable
* View::GetFocusTraversable() {
1259 FocusTraversable
* View::GetPaneFocusTraversable() {
1263 // Tooltips --------------------------------------------------------------------
1265 bool View::GetTooltipText(const gfx::Point
& p
, base::string16
* tooltip
) const {
1269 bool View::GetTooltipTextOrigin(const gfx::Point
& p
, gfx::Point
* loc
) const {
1273 // Context menus ---------------------------------------------------------------
1275 void View::ShowContextMenu(const gfx::Point
& p
,
1276 ui::MenuSourceType source_type
) {
1277 if (!context_menu_controller_
)
1280 context_menu_controller_
->ShowContextMenuForView(this, p
, source_type
);
1284 bool View::ShouldShowContextMenuOnMousePress() {
1285 return kContextMenuOnMousePress
;
1288 // Drag and drop ---------------------------------------------------------------
1290 bool View::GetDropFormats(
1292 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
1296 bool View::AreDropTypesRequired() {
1300 bool View::CanDrop(const OSExchangeData
& data
) {
1301 // TODO(sky): when I finish up migration, this should default to true.
1305 void View::OnDragEntered(const ui::DropTargetEvent
& event
) {
1308 int View::OnDragUpdated(const ui::DropTargetEvent
& event
) {
1309 return ui::DragDropTypes::DRAG_NONE
;
1312 void View::OnDragExited() {
1315 int View::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1316 return ui::DragDropTypes::DRAG_NONE
;
1319 void View::OnDragDone() {
1323 bool View::ExceededDragThreshold(const gfx::Vector2d
& delta
) {
1324 return (abs(delta
.x()) > GetHorizontalDragThreshold() ||
1325 abs(delta
.y()) > GetVerticalDragThreshold());
1328 // Accessibility----------------------------------------------------------------
1330 gfx::NativeViewAccessible
View::GetNativeViewAccessible() {
1331 if (!native_view_accessibility_
)
1332 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1333 if (native_view_accessibility_
)
1334 return native_view_accessibility_
->GetNativeObject();
1338 void View::NotifyAccessibilityEvent(
1339 ui::AXEvent event_type
,
1340 bool send_native_event
) {
1341 if (ViewsDelegate::views_delegate
)
1342 ViewsDelegate::views_delegate
->NotifyAccessibilityEvent(this, event_type
);
1344 if (send_native_event
&& GetWidget()) {
1345 if (!native_view_accessibility_
)
1346 native_view_accessibility_
= NativeViewAccessibility::Create(this);
1347 if (native_view_accessibility_
)
1348 native_view_accessibility_
->NotifyAccessibilityEvent(event_type
);
1352 // Scrolling -------------------------------------------------------------------
1354 void View::ScrollRectToVisible(const gfx::Rect
& rect
) {
1355 // We must take RTL UI mirroring into account when adjusting the position of
1358 gfx::Rect
scroll_rect(rect
);
1359 scroll_rect
.Offset(GetMirroredX(), y());
1360 parent_
->ScrollRectToVisible(scroll_rect
);
1364 int View::GetPageScrollIncrement(ScrollView
* scroll_view
,
1365 bool is_horizontal
, bool is_positive
) {
1369 int View::GetLineScrollIncrement(ScrollView
* scroll_view
,
1370 bool is_horizontal
, bool is_positive
) {
1374 ////////////////////////////////////////////////////////////////////////////////
1377 // Size and disposition --------------------------------------------------------
1379 void View::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1382 void View::PreferredSizeChanged() {
1385 parent_
->ChildPreferredSizeChanged(this);
1388 bool View::NeedsNotificationWhenVisibleBoundsChange() const {
1392 void View::OnVisibleBoundsChanged() {
1395 // Tree operations -------------------------------------------------------------
1397 void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails
& details
) {
1400 void View::VisibilityChanged(View
* starting_from
, bool is_visible
) {
1403 void View::NativeViewHierarchyChanged() {
1404 FocusManager
* focus_manager
= GetFocusManager();
1405 if (accelerator_focus_manager_
!= focus_manager
) {
1406 UnregisterAccelerators(true);
1409 RegisterPendingAccelerators();
1413 // Painting --------------------------------------------------------------------
1415 void View::PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1416 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1417 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1418 if (!child_at(i
)->layer())
1419 child_at(i
)->Paint(canvas
, cull_set
);
1422 void View::OnPaint(gfx::Canvas
* canvas
) {
1423 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1424 OnPaintBackground(canvas
);
1425 OnPaintBorder(canvas
);
1428 void View::OnPaintBackground(gfx::Canvas
* canvas
) {
1429 if (background_
.get()) {
1430 TRACE_EVENT2("views", "View::OnPaintBackground",
1431 "width", canvas
->sk_canvas()->getDevice()->width(),
1432 "height", canvas
->sk_canvas()->getDevice()->height());
1433 background_
->Paint(canvas
, this);
1437 void View::OnPaintBorder(gfx::Canvas
* canvas
) {
1438 if (border_
.get()) {
1439 TRACE_EVENT2("views", "View::OnPaintBorder",
1440 "width", canvas
->sk_canvas()->getDevice()->width(),
1441 "height", canvas
->sk_canvas()->getDevice()->height());
1442 border_
->Paint(*this, canvas
);
1446 bool View::IsPaintRoot() {
1447 return paint_to_layer_
|| !parent_
;
1450 // Accelerated Painting --------------------------------------------------------
1452 void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely
) {
1453 // This method should not have the side-effect of creating the layer.
1455 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely
);
1458 gfx::Vector2d
View::CalculateOffsetToAncestorWithLayer(
1459 ui::Layer
** layer_parent
) {
1462 *layer_parent
= layer();
1463 return gfx::Vector2d();
1466 return gfx::Vector2d();
1468 return gfx::Vector2d(GetMirroredX(), y()) +
1469 parent_
->CalculateOffsetToAncestorWithLayer(layer_parent
);
1472 void View::UpdateParentLayer() {
1476 ui::Layer
* parent_layer
= NULL
;
1477 gfx::Vector2d
offset(GetMirroredX(), y());
1480 offset
+= parent_
->CalculateOffsetToAncestorWithLayer(&parent_layer
);
1482 ReparentLayer(offset
, parent_layer
);
1485 void View::MoveLayerToParent(ui::Layer
* parent_layer
,
1486 const gfx::Point
& point
) {
1487 gfx::Point
local_point(point
);
1488 if (parent_layer
!= layer())
1489 local_point
.Offset(GetMirroredX(), y());
1490 if (layer() && parent_layer
!= layer()) {
1491 parent_layer
->Add(layer());
1492 SetLayerBounds(gfx::Rect(local_point
.x(), local_point
.y(),
1493 width(), height()));
1495 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1496 child_at(i
)->MoveLayerToParent(parent_layer
, local_point
);
1500 void View::UpdateLayerVisibility() {
1501 bool visible
= visible_
;
1502 for (const View
* v
= parent_
; visible
&& v
&& !v
->layer(); v
= v
->parent_
)
1503 visible
= v
->visible();
1505 UpdateChildLayerVisibility(visible
);
1508 void View::UpdateChildLayerVisibility(bool ancestor_visible
) {
1510 layer()->SetVisible(ancestor_visible
&& visible_
);
1512 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1513 child_at(i
)->UpdateChildLayerVisibility(ancestor_visible
&& visible_
);
1517 void View::UpdateChildLayerBounds(const gfx::Vector2d
& offset
) {
1519 SetLayerBounds(GetLocalBounds() + offset
);
1521 for (int i
= 0, count
= child_count(); i
< count
; ++i
) {
1522 View
* child
= child_at(i
);
1523 child
->UpdateChildLayerBounds(
1524 offset
+ gfx::Vector2d(child
->GetMirroredX(), child
->y()));
1529 void View::OnPaintLayer(gfx::Canvas
* canvas
) {
1530 if (!layer() || !layer()->fills_bounds_opaquely())
1531 canvas
->DrawColor(SK_ColorBLACK
, SkXfermode::kClear_Mode
);
1532 PaintCommon(canvas
, CullSet());
1535 void View::OnDeviceScaleFactorChanged(float device_scale_factor
) {
1536 snap_layer_to_pixel_boundary_
=
1537 (device_scale_factor
- std::floor(device_scale_factor
)) != 0.0f
;
1538 SnapLayerToPixelBoundary();
1539 // Repainting with new scale factor will paint the content at the right scale.
1542 base::Closure
View::PrepareForLayerBoundsChange() {
1543 return base::Closure();
1546 void View::ReorderLayers() {
1548 while (v
&& !v
->layer())
1551 Widget
* widget
= GetWidget();
1554 ui::Layer
* layer
= widget
->GetLayer();
1556 widget
->GetRootView()->ReorderChildLayers(layer
);
1559 v
->ReorderChildLayers(v
->layer());
1563 // Reorder the widget's child NativeViews in case a child NativeView is
1564 // associated with a view (eg via a NativeViewHost). Always do the
1565 // reordering because the associated NativeView's layer (if it has one)
1566 // is parented to the widget's layer regardless of whether the host view has
1567 // an ancestor with a layer.
1568 widget
->ReorderNativeViews();
1572 void View::ReorderChildLayers(ui::Layer
* parent_layer
) {
1573 if (layer() && layer() != parent_layer
) {
1574 DCHECK_EQ(parent_layer
, layer()->parent());
1575 parent_layer
->StackAtBottom(layer());
1577 // Iterate backwards through the children so that a child with a layer
1578 // which is further to the back is stacked above one which is further to
1580 for (Views::reverse_iterator
it(children_
.rbegin());
1581 it
!= children_
.rend(); ++it
) {
1582 (*it
)->ReorderChildLayers(parent_layer
);
1587 // Input -----------------------------------------------------------------------
1589 View::DragInfo
* View::GetDragInfo() {
1590 return parent_
? parent_
->GetDragInfo() : NULL
;
1593 // Focus -----------------------------------------------------------------------
1595 void View::OnFocus() {
1596 // TODO(beng): Investigate whether it's possible for us to move this to
1598 // By default, we clear the native focus. This ensures that no visible native
1599 // view as the focus and that we still receive keyboard inputs.
1600 FocusManager
* focus_manager
= GetFocusManager();
1602 focus_manager
->ClearNativeFocus();
1604 // TODO(beng): Investigate whether it's possible for us to move this to
1606 // Notify assistive technologies of the focus change.
1607 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
1610 void View::OnBlur() {
1613 void View::Focus() {
1621 // Tooltips --------------------------------------------------------------------
1623 void View::TooltipTextChanged() {
1624 Widget
* widget
= GetWidget();
1625 // TooltipManager may be null if there is a problem creating it.
1626 if (widget
&& widget
->GetTooltipManager())
1627 widget
->GetTooltipManager()->TooltipTextChanged(this);
1630 // Context menus ---------------------------------------------------------------
1632 gfx::Point
View::GetKeyboardContextMenuLocation() {
1633 gfx::Rect vis_bounds
= GetVisibleBounds();
1634 gfx::Point
screen_point(vis_bounds
.x() + vis_bounds
.width() / 2,
1635 vis_bounds
.y() + vis_bounds
.height() / 2);
1636 ConvertPointToScreen(this, &screen_point
);
1637 return screen_point
;
1640 // Drag and drop ---------------------------------------------------------------
1642 int View::GetDragOperations(const gfx::Point
& press_pt
) {
1643 return drag_controller_
?
1644 drag_controller_
->GetDragOperationsForView(this, press_pt
) :
1645 ui::DragDropTypes::DRAG_NONE
;
1648 void View::WriteDragData(const gfx::Point
& press_pt
, OSExchangeData
* data
) {
1649 DCHECK(drag_controller_
);
1650 drag_controller_
->WriteDragDataForView(this, press_pt
, data
);
1653 bool View::InDrag() {
1654 Widget
* widget
= GetWidget();
1655 return widget
? widget
->dragged_view() == this : false;
1658 int View::GetHorizontalDragThreshold() {
1659 // TODO(jennyz): This value may need to be adjusted for different platforms
1660 // and for different display density.
1661 return kDefaultHorizontalDragThreshold
;
1664 int View::GetVerticalDragThreshold() {
1665 // TODO(jennyz): This value may need to be adjusted for different platforms
1666 // and for different display density.
1667 return kDefaultVerticalDragThreshold
;
1670 // Debugging -------------------------------------------------------------------
1672 #if !defined(NDEBUG)
1674 std::string
View::PrintViewGraph(bool first
) {
1675 return DoPrintViewGraph(first
, this);
1678 std::string
View::DoPrintViewGraph(bool first
, View
* view_with_children
) {
1679 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1680 const size_t kMaxPointerStringLength
= 19;
1685 result
.append("digraph {\n");
1687 // Node characteristics.
1688 char p
[kMaxPointerStringLength
];
1690 const std::string
class_name(GetClassName());
1691 size_t base_name_index
= class_name
.find_last_of('/');
1692 if (base_name_index
== std::string::npos
)
1693 base_name_index
= 0;
1697 char bounds_buffer
[512];
1699 // Information about current node.
1700 base::snprintf(p
, arraysize(bounds_buffer
), "%p", view_with_children
);
1701 result
.append(" N");
1702 result
.append(p
+ 2);
1703 result
.append(" [label=\"");
1705 result
.append(class_name
.substr(base_name_index
).c_str());
1707 base::snprintf(bounds_buffer
,
1708 arraysize(bounds_buffer
),
1709 "\\n bounds: (%d, %d), (%dx%d)",
1714 result
.append(bounds_buffer
);
1716 gfx::DecomposedTransform decomp
;
1717 if (!GetTransform().IsIdentity() &&
1718 gfx::DecomposeTransform(&decomp
, GetTransform())) {
1719 base::snprintf(bounds_buffer
,
1720 arraysize(bounds_buffer
),
1721 "\\n translation: (%f, %f)",
1722 decomp
.translate
[0],
1723 decomp
.translate
[1]);
1724 result
.append(bounds_buffer
);
1726 base::snprintf(bounds_buffer
,
1727 arraysize(bounds_buffer
),
1728 "\\n rotation: %3.2f",
1729 std::acos(decomp
.quaternion
[3]) * 360.0 / M_PI
);
1730 result
.append(bounds_buffer
);
1732 base::snprintf(bounds_buffer
,
1733 arraysize(bounds_buffer
),
1734 "\\n scale: (%2.4f, %2.4f)",
1737 result
.append(bounds_buffer
);
1740 result
.append("\"");
1742 result
.append(", shape=box");
1744 if (layer()->has_external_content())
1745 result
.append(", color=green");
1747 result
.append(", color=red");
1749 if (layer()->fills_bounds_opaquely())
1750 result
.append(", style=filled");
1752 result
.append("]\n");
1756 char pp
[kMaxPointerStringLength
];
1758 base::snprintf(pp
, kMaxPointerStringLength
, "%p", parent_
);
1759 result
.append(" N");
1760 result
.append(pp
+ 2);
1761 result
.append(" -> N");
1762 result
.append(p
+ 2);
1763 result
.append("\n");
1767 for (int i
= 0, count
= view_with_children
->child_count(); i
< count
; ++i
)
1768 result
.append(view_with_children
->child_at(i
)->PrintViewGraph(false));
1771 result
.append("}\n");
1777 ////////////////////////////////////////////////////////////////////////////////
1780 // DropInfo --------------------------------------------------------------------
1782 void View::DragInfo::Reset() {
1783 possible_drag
= false;
1784 start_pt
= gfx::Point();
1787 void View::DragInfo::PossibleDrag(const gfx::Point
& p
) {
1788 possible_drag
= true;
1792 // Painting --------------------------------------------------------------------
1794 void View::SchedulePaintBoundsChanged(SchedulePaintType type
) {
1795 // If we have a layer and the View's size did not change, we do not need to
1796 // schedule any paints since the layer will be redrawn at its new location
1797 // during the next Draw() cycle in the compositor.
1798 if (!layer() || type
== SCHEDULE_PAINT_SIZE_CHANGED
) {
1799 // Otherwise, if the size changes or we don't have a layer then we need to
1800 // use SchedulePaint to invalidate the area occupied by the View.
1802 } else if (parent_
&& type
== SCHEDULE_PAINT_SIZE_SAME
) {
1803 // The compositor doesn't Draw() until something on screen changes, so
1804 // if our position changes but nothing is being animated on screen, then
1805 // tell the compositor to redraw the scene. We know layer() exists due to
1806 // the above if clause.
1807 layer()->ScheduleDraw();
1811 void View::PaintCommon(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
1816 // If the View we are about to paint requested the canvas to be flipped, we
1817 // should change the transform appropriately.
1818 // The canvas mirroring is undone once the View is done painting so that we
1819 // don't pass the canvas with the mirrored transform to Views that didn't
1820 // request the canvas to be flipped.
1821 gfx::ScopedCanvas
scoped(canvas
);
1822 if (FlipCanvasOnPaintForRTLUI()) {
1823 canvas
->Translate(gfx::Vector2d(width(), 0));
1824 canvas
->Scale(-1, 1);
1830 PaintChildren(canvas
, cull_set
);
1833 // Tree operations -------------------------------------------------------------
1835 void View::DoRemoveChildView(View
* view
,
1836 bool update_focus_cycle
,
1837 bool update_tool_tip
,
1838 bool delete_removed_view
,
1842 const Views::iterator
i(std::find(children_
.begin(), children_
.end(), view
));
1843 scoped_ptr
<View
> view_to_be_deleted
;
1844 if (i
!= children_
.end()) {
1845 if (update_focus_cycle
) {
1846 // Let's remove the view from the focus traversal.
1847 View
* next_focusable
= view
->next_focusable_view_
;
1848 View
* prev_focusable
= view
->previous_focusable_view_
;
1850 prev_focusable
->next_focusable_view_
= next_focusable
;
1852 next_focusable
->previous_focusable_view_
= prev_focusable
;
1856 UnregisterChildrenForVisibleBoundsNotification(view
);
1857 if (view
->visible())
1858 view
->SchedulePaint();
1859 GetWidget()->NotifyWillRemoveView(view
);
1862 // Remove the bounds of this child and any of its descendants from our
1863 // paint root bounds tree.
1864 BoundsTree
* bounds_tree
= GetBoundsTreeFromPaintRoot();
1866 view
->RemoveRootBounds(bounds_tree
);
1868 view
->PropagateRemoveNotifications(this, new_parent
);
1869 view
->parent_
= NULL
;
1870 view
->UpdateLayerVisibility();
1872 if (delete_removed_view
&& !view
->owned_by_client_
)
1873 view_to_be_deleted
.reset(view
);
1878 if (update_tool_tip
)
1881 if (layout_manager_
.get())
1882 layout_manager_
->ViewRemoved(this, view
);
1885 void View::PropagateRemoveNotifications(View
* old_parent
, View
* new_parent
) {
1886 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1887 child_at(i
)->PropagateRemoveNotifications(old_parent
, new_parent
);
1889 ViewHierarchyChangedDetails
details(false, old_parent
, this, new_parent
);
1890 for (View
* v
= this; v
; v
= v
->parent_
)
1891 v
->ViewHierarchyChangedImpl(true, details
);
1894 void View::PropagateAddNotifications(
1895 const ViewHierarchyChangedDetails
& details
) {
1896 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1897 child_at(i
)->PropagateAddNotifications(details
);
1898 ViewHierarchyChangedImpl(true, details
);
1901 void View::PropagateNativeViewHierarchyChanged() {
1902 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1903 child_at(i
)->PropagateNativeViewHierarchyChanged();
1904 NativeViewHierarchyChanged();
1907 void View::ViewHierarchyChangedImpl(
1908 bool register_accelerators
,
1909 const ViewHierarchyChangedDetails
& details
) {
1910 if (register_accelerators
) {
1911 if (details
.is_add
) {
1912 // If you get this registration, you are part of a subtree that has been
1913 // added to the view hierarchy.
1914 if (GetFocusManager())
1915 RegisterPendingAccelerators();
1917 if (details
.child
== this)
1918 UnregisterAccelerators(true);
1922 if (details
.is_add
&& layer() && !layer()->parent()) {
1923 UpdateParentLayer();
1924 Widget
* widget
= GetWidget();
1926 widget
->UpdateRootLayers();
1927 } else if (!details
.is_add
&& details
.child
== this) {
1928 // Make sure the layers belonging to the subtree rooted at |child| get
1929 // removed from layers that do not belong in the same subtree.
1931 Widget
* widget
= GetWidget();
1933 widget
->UpdateRootLayers();
1936 ViewHierarchyChanged(details
);
1937 details
.parent
->needs_layout_
= true;
1940 void View::PropagateNativeThemeChanged(const ui::NativeTheme
* theme
) {
1941 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1942 child_at(i
)->PropagateNativeThemeChanged(theme
);
1943 OnNativeThemeChanged(theme
);
1946 // Size and disposition --------------------------------------------------------
1948 void View::PropagateVisibilityNotifications(View
* start
, bool is_visible
) {
1949 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
1950 child_at(i
)->PropagateVisibilityNotifications(start
, is_visible
);
1951 VisibilityChangedImpl(start
, is_visible
);
1954 void View::VisibilityChangedImpl(View
* starting_from
, bool is_visible
) {
1955 VisibilityChanged(starting_from
, is_visible
);
1958 void View::BoundsChanged(const gfx::Rect
& previous_bounds
) {
1959 // Mark our bounds as dirty for the paint root, also see if we need to
1960 // recompute our children's bounds due to origin change.
1961 bool origin_changed
=
1962 previous_bounds
.OffsetFromOrigin() != bounds_
.OffsetFromOrigin();
1963 SetRootBoundsDirty(origin_changed
);
1966 // Paint the new bounds.
1967 SchedulePaintBoundsChanged(
1968 bounds_
.size() == previous_bounds
.size() ? SCHEDULE_PAINT_SIZE_SAME
:
1969 SCHEDULE_PAINT_SIZE_CHANGED
);
1974 SetLayerBounds(GetLocalBounds() +
1975 gfx::Vector2d(GetMirroredX(), y()) +
1976 parent_
->CalculateOffsetToAncestorWithLayer(NULL
));
1978 SetLayerBounds(bounds_
);
1981 // If our bounds have changed, then any descendant layer bounds may have
1982 // changed. Update them accordingly.
1983 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
1986 OnBoundsChanged(previous_bounds
);
1988 if (previous_bounds
.size() != size()) {
1989 needs_layout_
= false;
1993 if (NeedsNotificationWhenVisibleBoundsChange())
1994 OnVisibleBoundsChanged();
1996 // Notify interested Views that visible bounds within the root view may have
1998 if (descendants_to_notify_
.get()) {
1999 for (Views::iterator
i(descendants_to_notify_
->begin());
2000 i
!= descendants_to_notify_
->end(); ++i
) {
2001 (*i
)->OnVisibleBoundsChanged();
2007 void View::RegisterChildrenForVisibleBoundsNotification(View
* view
) {
2008 if (view
->NeedsNotificationWhenVisibleBoundsChange())
2009 view
->RegisterForVisibleBoundsNotification();
2010 for (int i
= 0; i
< view
->child_count(); ++i
)
2011 RegisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
2015 void View::UnregisterChildrenForVisibleBoundsNotification(View
* view
) {
2016 if (view
->NeedsNotificationWhenVisibleBoundsChange())
2017 view
->UnregisterForVisibleBoundsNotification();
2018 for (int i
= 0; i
< view
->child_count(); ++i
)
2019 UnregisterChildrenForVisibleBoundsNotification(view
->child_at(i
));
2022 void View::RegisterForVisibleBoundsNotification() {
2023 if (registered_for_visible_bounds_notification_
)
2026 registered_for_visible_bounds_notification_
= true;
2027 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
2028 ancestor
->AddDescendantToNotify(this);
2031 void View::UnregisterForVisibleBoundsNotification() {
2032 if (!registered_for_visible_bounds_notification_
)
2035 registered_for_visible_bounds_notification_
= false;
2036 for (View
* ancestor
= parent_
; ancestor
; ancestor
= ancestor
->parent_
)
2037 ancestor
->RemoveDescendantToNotify(this);
2040 void View::AddDescendantToNotify(View
* view
) {
2042 if (!descendants_to_notify_
.get())
2043 descendants_to_notify_
.reset(new Views
);
2044 descendants_to_notify_
->push_back(view
);
2047 void View::RemoveDescendantToNotify(View
* view
) {
2048 DCHECK(view
&& descendants_to_notify_
.get());
2049 Views::iterator
i(std::find(
2050 descendants_to_notify_
->begin(), descendants_to_notify_
->end(), view
));
2051 DCHECK(i
!= descendants_to_notify_
->end());
2052 descendants_to_notify_
->erase(i
);
2053 if (descendants_to_notify_
->empty())
2054 descendants_to_notify_
.reset();
2057 void View::SetLayerBounds(const gfx::Rect
& bounds
) {
2058 layer()->SetBounds(bounds
);
2059 SnapLayerToPixelBoundary();
2062 void View::SetRootBoundsDirty(bool origin_changed
) {
2063 root_bounds_dirty_
= true;
2065 if (origin_changed
) {
2066 // Inform our children that their root bounds are dirty, as their relative
2067 // coordinates in paint root space have changed since ours have changed.
2068 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end();
2070 if (!(*i
)->IsPaintRoot())
2071 (*i
)->SetRootBoundsDirty(origin_changed
);
2076 void View::UpdateRootBounds(BoundsTree
* tree
, const gfx::Vector2d
& offset
) {
2077 // No need to recompute bounds if we haven't flagged ours as dirty.
2078 TRACE_EVENT1("views", "View::UpdateRootBounds", "class", GetClassName());
2080 // Add our own offset to the provided offset, for our own bounds update and
2081 // for propagation to our children if needed.
2082 gfx::Vector2d view_offset
= offset
+ GetMirroredBounds().OffsetFromOrigin();
2084 // If our bounds have changed we must re-insert our new bounds to the tree.
2085 if (root_bounds_dirty_
) {
2086 root_bounds_dirty_
= false;
2088 view_offset
.x(), view_offset
.y(), bounds_
.width(), bounds_
.height());
2089 tree
->Insert(bounds
, reinterpret_cast<intptr_t>(this));
2092 // Update our children's bounds if needed.
2093 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2094 // We don't descend in to layer views for bounds recomputation, as they
2095 // manage their own RTree as paint roots.
2096 if (!(*i
)->IsPaintRoot())
2097 (*i
)->UpdateRootBounds(tree
, view_offset
);
2101 void View::RemoveRootBounds(BoundsTree
* tree
) {
2102 tree
->Remove(reinterpret_cast<intptr_t>(this));
2104 root_bounds_dirty_
= true;
2106 for (Views::const_iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2107 if (!(*i
)->IsPaintRoot())
2108 (*i
)->RemoveRootBounds(tree
);
2112 View::BoundsTree
* View::GetBoundsTreeFromPaintRoot() {
2113 BoundsTree
* bounds_tree
= bounds_tree_
.get();
2114 View
* paint_root
= this;
2115 while (!bounds_tree
&& !paint_root
->IsPaintRoot()) {
2116 // Assumption is that if IsPaintRoot() is false then parent_ is valid.
2118 paint_root
= paint_root
->parent_
;
2119 bounds_tree
= paint_root
->bounds_tree_
.get();
2125 // Transformations -------------------------------------------------------------
2127 bool View::GetTransformRelativeTo(const View
* ancestor
,
2128 gfx::Transform
* transform
) const {
2129 const View
* p
= this;
2131 while (p
&& p
!= ancestor
) {
2132 transform
->ConcatTransform(p
->GetTransform());
2133 gfx::Transform translation
;
2134 translation
.Translate(static_cast<float>(p
->GetMirroredX()),
2135 static_cast<float>(p
->y()));
2136 transform
->ConcatTransform(translation
);
2141 return p
== ancestor
;
2144 // Coordinate conversion -------------------------------------------------------
2146 bool View::ConvertPointForAncestor(const View
* ancestor
,
2147 gfx::Point
* point
) const {
2148 gfx::Transform trans
;
2149 // TODO(sad): Have some way of caching the transformation results.
2150 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2151 gfx::Point3F
p(*point
);
2152 trans
.TransformPoint(&p
);
2153 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2157 bool View::ConvertPointFromAncestor(const View
* ancestor
,
2158 gfx::Point
* point
) const {
2159 gfx::Transform trans
;
2160 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2161 gfx::Point3F
p(*point
);
2162 trans
.TransformPointReverse(&p
);
2163 *point
= gfx::ToFlooredPoint(p
.AsPointF());
2167 bool View::ConvertRectForAncestor(const View
* ancestor
,
2168 gfx::RectF
* rect
) const {
2169 gfx::Transform trans
;
2170 // TODO(sad): Have some way of caching the transformation results.
2171 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2172 trans
.TransformRect(rect
);
2176 bool View::ConvertRectFromAncestor(const View
* ancestor
,
2177 gfx::RectF
* rect
) const {
2178 gfx::Transform trans
;
2179 bool result
= GetTransformRelativeTo(ancestor
, &trans
);
2180 trans
.TransformRectReverse(rect
);
2184 // Accelerated painting --------------------------------------------------------
2186 void View::CreateLayer() {
2187 // A new layer is being created for the view. So all the layers of the
2188 // sub-tree can inherit the visibility of the corresponding view.
2189 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2190 child_at(i
)->UpdateChildLayerVisibility(true);
2192 SetLayer(new ui::Layer());
2193 layer()->set_delegate(this);
2194 #if !defined(NDEBUG)
2195 layer()->set_name(GetClassName());
2198 UpdateParentLayers();
2199 UpdateLayerVisibility();
2201 // The new layer needs to be ordered in the layer tree according
2202 // to the view tree. Children of this layer were added in order
2203 // in UpdateParentLayers().
2205 parent()->ReorderLayers();
2207 Widget
* widget
= GetWidget();
2209 widget
->UpdateRootLayers();
2212 void View::UpdateParentLayers() {
2213 // Attach all top-level un-parented layers.
2214 if (layer() && !layer()->parent()) {
2215 UpdateParentLayer();
2217 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2218 child_at(i
)->UpdateParentLayers();
2222 void View::OrphanLayers() {
2224 if (layer()->parent())
2225 layer()->parent()->Remove(layer());
2227 // The layer belonging to this View has already been orphaned. It is not
2228 // necessary to orphan the child layers.
2231 for (int i
= 0, count
= child_count(); i
< count
; ++i
)
2232 child_at(i
)->OrphanLayers();
2235 void View::ReparentLayer(const gfx::Vector2d
& offset
, ui::Layer
* parent_layer
) {
2236 layer()->SetBounds(GetLocalBounds() + offset
);
2237 DCHECK_NE(layer(), parent_layer
);
2239 parent_layer
->Add(layer());
2240 layer()->SchedulePaint(GetLocalBounds());
2241 MoveLayerToParent(layer(), gfx::Point());
2244 void View::DestroyLayer() {
2245 ui::Layer
* new_parent
= layer()->parent();
2246 std::vector
<ui::Layer
*> children
= layer()->children();
2247 for (size_t i
= 0; i
< children
.size(); ++i
) {
2248 layer()->Remove(children
[i
]);
2250 new_parent
->Add(children
[i
]);
2253 LayerOwner::DestroyLayer();
2258 UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(NULL
));
2262 Widget
* widget
= GetWidget();
2264 widget
->UpdateRootLayers();
2267 // Input -----------------------------------------------------------------------
2269 bool View::ProcessMousePressed(const ui::MouseEvent
& event
) {
2270 int drag_operations
=
2271 (enabled_
&& event
.IsOnlyLeftMouseButton() &&
2272 HitTestPoint(event
.location())) ?
2273 GetDragOperations(event
.location()) : 0;
2274 ContextMenuController
* context_menu_controller
= event
.IsRightMouseButton() ?
2275 context_menu_controller_
: 0;
2276 View::DragInfo
* drag_info
= GetDragInfo();
2278 // TODO(sky): for debugging 360238.
2280 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2281 kContextMenuOnMousePress
&& HitTestPoint(event
.location())) {
2282 ViewStorage
* view_storage
= ViewStorage::GetInstance();
2283 storage_id
= view_storage
->CreateStorageID();
2284 view_storage
->StoreView(storage_id
, this);
2287 const bool enabled
= enabled_
;
2288 const bool result
= OnMousePressed(event
);
2293 if (event
.IsOnlyRightMouseButton() && context_menu_controller
&&
2294 kContextMenuOnMousePress
) {
2295 // Assume that if there is a context menu controller we won't be deleted
2296 // from mouse pressed.
2297 gfx::Point
location(event
.location());
2298 if (HitTestPoint(location
)) {
2299 if (storage_id
!= 0)
2300 CHECK_EQ(this, ViewStorage::GetInstance()->RetrieveView(storage_id
));
2301 ConvertPointToScreen(this, &location
);
2302 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2307 // WARNING: we may have been deleted, don't use any View variables.
2308 if (drag_operations
!= ui::DragDropTypes::DRAG_NONE
) {
2309 drag_info
->PossibleDrag(event
.location());
2312 return !!context_menu_controller
|| result
;
2315 bool View::ProcessMouseDragged(const ui::MouseEvent
& event
) {
2316 // Copy the field, that way if we're deleted after drag and drop no harm is
2318 ContextMenuController
* context_menu_controller
= context_menu_controller_
;
2319 const bool possible_drag
= GetDragInfo()->possible_drag
;
2320 if (possible_drag
&&
2321 ExceededDragThreshold(GetDragInfo()->start_pt
- event
.location()) &&
2322 (!drag_controller_
||
2323 drag_controller_
->CanStartDragForView(
2324 this, GetDragInfo()->start_pt
, event
.location()))) {
2325 DoDrag(event
, GetDragInfo()->start_pt
,
2326 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
);
2328 if (OnMouseDragged(event
))
2330 // Fall through to return value based on context menu controller.
2332 // WARNING: we may have been deleted.
2333 return (context_menu_controller
!= NULL
) || possible_drag
;
2336 void View::ProcessMouseReleased(const ui::MouseEvent
& event
) {
2337 if (!kContextMenuOnMousePress
&& context_menu_controller_
&&
2338 event
.IsOnlyRightMouseButton()) {
2339 // Assume that if there is a context menu controller we won't be deleted
2340 // from mouse released.
2341 gfx::Point
location(event
.location());
2342 OnMouseReleased(event
);
2343 if (HitTestPoint(location
)) {
2344 ConvertPointToScreen(this, &location
);
2345 ShowContextMenu(location
, ui::MENU_SOURCE_MOUSE
);
2348 OnMouseReleased(event
);
2350 // WARNING: we may have been deleted.
2353 // Accelerators ----------------------------------------------------------------
2355 void View::RegisterPendingAccelerators() {
2356 if (!accelerators_
.get() ||
2357 registered_accelerator_count_
== accelerators_
->size()) {
2358 // No accelerators are waiting for registration.
2363 // The view is not yet attached to a widget, defer registration until then.
2367 accelerator_focus_manager_
= GetFocusManager();
2368 if (!accelerator_focus_manager_
) {
2369 // Some crash reports seem to show that we may get cases where we have no
2370 // focus manager (see bug #1291225). This should never be the case, just
2371 // making sure we don't crash.
2375 for (std::vector
<ui::Accelerator
>::const_iterator
i(
2376 accelerators_
->begin() + registered_accelerator_count_
);
2377 i
!= accelerators_
->end(); ++i
) {
2378 accelerator_focus_manager_
->RegisterAccelerator(
2379 *i
, ui::AcceleratorManager::kNormalPriority
, this);
2381 registered_accelerator_count_
= accelerators_
->size();
2384 void View::UnregisterAccelerators(bool leave_data_intact
) {
2385 if (!accelerators_
.get())
2389 if (accelerator_focus_manager_
) {
2390 accelerator_focus_manager_
->UnregisterAccelerators(this);
2391 accelerator_focus_manager_
= NULL
;
2393 if (!leave_data_intact
) {
2394 accelerators_
->clear();
2395 accelerators_
.reset();
2397 registered_accelerator_count_
= 0;
2401 // Focus -----------------------------------------------------------------------
2403 void View::InitFocusSiblings(View
* v
, int index
) {
2404 int count
= child_count();
2407 v
->next_focusable_view_
= NULL
;
2408 v
->previous_focusable_view_
= NULL
;
2410 if (index
== count
) {
2411 // We are inserting at the end, but the end of the child list may not be
2412 // the last focusable element. Let's try to find an element with no next
2413 // focusable element to link to.
2414 View
* last_focusable_view
= NULL
;
2415 for (Views::iterator
i(children_
.begin()); i
!= children_
.end(); ++i
) {
2416 if (!(*i
)->next_focusable_view_
) {
2417 last_focusable_view
= *i
;
2421 if (last_focusable_view
== NULL
) {
2422 // Hum... there is a cycle in the focus list. Let's just insert ourself
2423 // after the last child.
2424 View
* prev
= children_
[index
- 1];
2425 v
->previous_focusable_view_
= prev
;
2426 v
->next_focusable_view_
= prev
->next_focusable_view_
;
2427 prev
->next_focusable_view_
->previous_focusable_view_
= v
;
2428 prev
->next_focusable_view_
= v
;
2430 last_focusable_view
->next_focusable_view_
= v
;
2431 v
->next_focusable_view_
= NULL
;
2432 v
->previous_focusable_view_
= last_focusable_view
;
2435 View
* prev
= children_
[index
]->GetPreviousFocusableView();
2436 v
->previous_focusable_view_
= prev
;
2437 v
->next_focusable_view_
= children_
[index
];
2439 prev
->next_focusable_view_
= v
;
2440 children_
[index
]->previous_focusable_view_
= v
;
2445 // System events ---------------------------------------------------------------
2447 void View::PropagateThemeChanged() {
2448 for (int i
= child_count() - 1; i
>= 0; --i
)
2449 child_at(i
)->PropagateThemeChanged();
2453 void View::PropagateLocaleChanged() {
2454 for (int i
= child_count() - 1; i
>= 0; --i
)
2455 child_at(i
)->PropagateLocaleChanged();
2459 // Tooltips --------------------------------------------------------------------
2461 void View::UpdateTooltip() {
2462 Widget
* widget
= GetWidget();
2463 // TODO(beng): The TooltipManager NULL check can be removed when we
2464 // consolidate Init() methods and make views_unittests Init() all
2465 // Widgets that it uses.
2466 if (widget
&& widget
->GetTooltipManager())
2467 widget
->GetTooltipManager()->UpdateTooltip();
2470 // Drag and drop ---------------------------------------------------------------
2472 bool View::DoDrag(const ui::LocatedEvent
& event
,
2473 const gfx::Point
& press_pt
,
2474 ui::DragDropTypes::DragEventSource source
) {
2475 int drag_operations
= GetDragOperations(press_pt
);
2476 if (drag_operations
== ui::DragDropTypes::DRAG_NONE
)
2479 Widget
* widget
= GetWidget();
2480 // We should only start a drag from an event, implying we have a widget.
2483 // Don't attempt to start a drag while in the process of dragging. This is
2484 // especially important on X where we can get multiple mouse move events when
2485 // we start the drag.
2486 if (widget
->dragged_view())
2489 OSExchangeData data
;
2490 WriteDragData(press_pt
, &data
);
2492 // Message the RootView to do the drag and drop. That way if we're removed
2493 // the RootView can detect it and avoid calling us back.
2494 gfx::Point
widget_location(event
.location());
2495 ConvertPointToWidget(this, &widget_location
);
2496 widget
->RunShellDrag(this, data
, widget_location
, drag_operations
, source
);
2497 // WARNING: we may have been deleted.
2501 } // namespace views