Migrate away from the PrefMetricsService-based device ID in PrefHashCalculator.
[chromium-blink-merge.git] / ui / views / widget / root_view.cc
blob4fdd553af643dd8e5b386f4240774bb76cabd29f
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 #include "ui/views/widget/root_view.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/cursor/cursor.h"
13 #include "ui/base/dragdrop/drag_drop_types.h"
14 #include "ui/base/ui_base_switches_util.h"
15 #include "ui/compositor/layer.h"
16 #include "ui/events/event.h"
17 #include "ui/events/keycodes/keyboard_codes.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/views/drag_controller.h"
20 #include "ui/views/focus/view_storage.h"
21 #include "ui/views/layout/fill_layout.h"
22 #include "ui/views/view_targeter.h"
23 #include "ui/views/views_switches.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/widget/widget_delegate.h"
27 typedef ui::EventDispatchDetails DispatchDetails;
29 namespace views {
30 namespace internal {
32 namespace {
34 enum EventType {
35 EVENT_ENTER,
36 EVENT_EXIT
39 class MouseEnterExitEvent : public ui::MouseEvent {
40 public:
41 MouseEnterExitEvent(const ui::MouseEvent& event, ui::EventType type)
42 : ui::MouseEvent(event,
43 static_cast<View*>(NULL),
44 static_cast<View*>(NULL)) {
45 DCHECK(type == ui::ET_MOUSE_ENTERED ||
46 type == ui::ET_MOUSE_EXITED);
47 SetType(type);
50 virtual ~MouseEnterExitEvent() {}
53 } // namespace
55 // This event handler receives events in the pre-target phase and takes care of
56 // the following:
57 // - Shows keyboard-triggered context menus.
58 class PreEventDispatchHandler : public ui::EventHandler {
59 public:
60 explicit PreEventDispatchHandler(View* owner)
61 : owner_(owner) {
63 virtual ~PreEventDispatchHandler() {}
65 private:
66 // ui::EventHandler:
67 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
68 CHECK_EQ(ui::EP_PRETARGET, event->phase());
69 if (event->handled())
70 return;
72 View* v = NULL;
73 if (owner_->GetFocusManager()) // Can be NULL in unittests.
74 v = owner_->GetFocusManager()->GetFocusedView();
76 // Special case to handle keyboard-triggered context menus.
77 if (v && v->enabled() && ((event->key_code() == ui::VKEY_APPS) ||
78 (event->key_code() == ui::VKEY_F10 && event->IsShiftDown()))) {
79 // Clamp the menu location within the visible bounds of each ancestor view
80 // to avoid showing the menu over a completely different view or window.
81 gfx::Point location = v->GetKeyboardContextMenuLocation();
82 for (View* parent = v->parent(); parent; parent = parent->parent()) {
83 const gfx::Rect& parent_bounds = parent->GetBoundsInScreen();
84 location.SetToMax(parent_bounds.origin());
85 location.SetToMin(parent_bounds.bottom_right());
87 v->ShowContextMenu(location, ui::MENU_SOURCE_KEYBOARD);
88 event->StopPropagation();
92 View* owner_;
94 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler);
97 // This event handler receives events in the post-target phase and takes care of
98 // the following:
99 // - Generates context menu, or initiates drag-and-drop, from gesture events.
100 class PostEventDispatchHandler : public ui::EventHandler {
101 public:
102 PostEventDispatchHandler()
103 : touch_dnd_enabled_(::switches::IsTouchDragDropEnabled()) {
105 virtual ~PostEventDispatchHandler() {}
107 private:
108 // Overridden from ui::EventHandler:
109 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
110 DCHECK_EQ(ui::EP_POSTTARGET, event->phase());
111 if (event->handled())
112 return;
114 View* target = static_cast<View*>(event->target());
115 gfx::Point location = event->location();
116 if (touch_dnd_enabled_ &&
117 event->type() == ui::ET_GESTURE_LONG_PRESS &&
118 (!target->drag_controller() ||
119 target->drag_controller()->CanStartDragForView(
120 target, location, location))) {
121 if (target->DoDrag(*event, location,
122 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH)) {
123 event->StopPropagation();
124 return;
128 if (target->context_menu_controller() &&
129 (event->type() == ui::ET_GESTURE_LONG_PRESS ||
130 event->type() == ui::ET_GESTURE_LONG_TAP ||
131 event->type() == ui::ET_GESTURE_TWO_FINGER_TAP)) {
132 gfx::Point screen_location(location);
133 View::ConvertPointToScreen(target, &screen_location);
134 target->ShowContextMenu(screen_location, ui::MENU_SOURCE_TOUCH);
135 event->StopPropagation();
139 bool touch_dnd_enabled_;
141 DISALLOW_COPY_AND_ASSIGN(PostEventDispatchHandler);
144 // static
145 const char RootView::kViewClassName[] = "RootView";
147 ////////////////////////////////////////////////////////////////////////////////
148 // RootView, public:
150 // Creation and lifetime -------------------------------------------------------
152 RootView::RootView(Widget* widget)
153 : widget_(widget),
154 mouse_pressed_handler_(NULL),
155 mouse_move_handler_(NULL),
156 last_click_handler_(NULL),
157 explicit_mouse_handler_(false),
158 last_mouse_event_flags_(0),
159 last_mouse_event_x_(-1),
160 last_mouse_event_y_(-1),
161 gesture_handler_(NULL),
162 scroll_gesture_handler_(NULL),
163 pre_dispatch_handler_(new internal::PreEventDispatchHandler(this)),
164 post_dispatch_handler_(new internal::PostEventDispatchHandler),
165 focus_search_(this, false, false),
166 focus_traversable_parent_(NULL),
167 focus_traversable_parent_view_(NULL),
168 event_dispatch_target_(NULL),
169 old_dispatch_target_(NULL) {
170 AddPreTargetHandler(pre_dispatch_handler_.get());
171 AddPostTargetHandler(post_dispatch_handler_.get());
172 SetEventTargeter(scoped_ptr<ViewTargeter>(new ViewTargeter(this)));
175 RootView::~RootView() {
176 // If we have children remove them explicitly so to make sure a remove
177 // notification is sent for each one of them.
178 if (has_children())
179 RemoveAllChildViews(true);
182 // Tree operations -------------------------------------------------------------
184 void RootView::SetContentsView(View* contents_view) {
185 DCHECK(contents_view && GetWidget()->native_widget()) <<
186 "Can't be called until after the native widget is created!";
187 // The ContentsView must be set up _after_ the window is created so that its
188 // Widget pointer is valid.
189 SetLayoutManager(new FillLayout);
190 if (has_children())
191 RemoveAllChildViews(true);
192 AddChildView(contents_view);
194 // Force a layout now, since the attached hierarchy won't be ready for the
195 // containing window's bounds. Note that we call Layout directly rather than
196 // calling the widget's size changed handler, since the RootView's bounds may
197 // not have changed, which will cause the Layout not to be done otherwise.
198 Layout();
201 View* RootView::GetContentsView() {
202 return child_count() > 0 ? child_at(0) : NULL;
205 void RootView::NotifyNativeViewHierarchyChanged() {
206 PropagateNativeViewHierarchyChanged();
209 // Focus -----------------------------------------------------------------------
211 void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) {
212 DCHECK(focus_traversable != this);
213 focus_traversable_parent_ = focus_traversable;
216 void RootView::SetFocusTraversableParentView(View* view) {
217 focus_traversable_parent_view_ = view;
220 // System events ---------------------------------------------------------------
222 void RootView::ThemeChanged() {
223 View::PropagateThemeChanged();
226 void RootView::LocaleChanged() {
227 View::PropagateLocaleChanged();
230 ////////////////////////////////////////////////////////////////////////////////
231 // RootView, FocusTraversable implementation:
233 FocusSearch* RootView::GetFocusSearch() {
234 return &focus_search_;
237 FocusTraversable* RootView::GetFocusTraversableParent() {
238 return focus_traversable_parent_;
241 View* RootView::GetFocusTraversableParentView() {
242 return focus_traversable_parent_view_;
245 ////////////////////////////////////////////////////////////////////////////////
246 // RootView, ui::EventProcessor overrides:
248 ui::EventTarget* RootView::GetRootTarget() {
249 return this;
252 ui::EventDispatchDetails RootView::OnEventFromSource(ui::Event* event) {
253 // TODO(tdanderson): Replace the calls to Dispatch*Event() with calls to
254 // EventProcessor::OnEventFromSource() once the code for
255 // that event type has been refactored, and then
256 // eventually remove this function altogether. See
257 // crbug.com/348083.
259 if (event->IsKeyEvent())
260 return EventProcessor::OnEventFromSource(event);
262 if (event->IsScrollEvent())
263 return EventProcessor::OnEventFromSource(event);
265 if (event->IsGestureEvent()) {
266 // Ignore subsequent gesture scroll events if no handler was set for a
267 // ui::ET_GESTURE_SCROLL_BEGIN event.
268 if (!gesture_handler_ &&
269 (event->type() == ui::ET_GESTURE_SCROLL_UPDATE ||
270 event->type() == ui::ET_GESTURE_SCROLL_END ||
271 event->type() == ui::ET_SCROLL_FLING_START)) {
272 return DispatchDetails();
275 DispatchGestureEvent(event->AsGestureEvent());
276 return DispatchDetails();
279 if (event->IsTouchEvent())
280 NOTREACHED() << "Touch events should not be sent to RootView.";
282 if (event->IsMouseEvent())
283 NOTREACHED() << "Should not be called with a MouseEvent.";
285 return DispatchDetails();
288 ////////////////////////////////////////////////////////////////////////////////
289 // RootView, View overrides:
291 const Widget* RootView::GetWidget() const {
292 return widget_;
295 Widget* RootView::GetWidget() {
296 return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget());
299 bool RootView::IsDrawn() const {
300 return visible();
303 void RootView::Layout() {
304 View::Layout();
305 widget_->OnRootViewLayout();
308 const char* RootView::GetClassName() const {
309 return kViewClassName;
312 void RootView::SchedulePaintInRect(const gfx::Rect& rect) {
313 if (layer()) {
314 layer()->SchedulePaint(rect);
315 } else {
316 gfx::Rect xrect = ConvertRectToParent(rect);
317 gfx::Rect invalid_rect = gfx::IntersectRects(GetLocalBounds(), xrect);
318 if (!invalid_rect.IsEmpty())
319 widget_->SchedulePaintInRect(invalid_rect);
323 bool RootView::OnMousePressed(const ui::MouseEvent& event) {
324 UpdateCursor(event);
325 SetMouseLocationAndFlags(event);
327 // If mouse_pressed_handler_ is non null, we are currently processing
328 // a pressed -> drag -> released session. In that case we send the
329 // event to mouse_pressed_handler_
330 if (mouse_pressed_handler_) {
331 ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
332 mouse_pressed_handler_);
333 drag_info_.Reset();
334 ui::EventDispatchDetails dispatch_details =
335 DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event);
336 if (dispatch_details.dispatcher_destroyed)
337 return true;
338 return true;
340 DCHECK(!explicit_mouse_handler_);
342 bool hit_disabled_view = false;
343 // Walk up the tree until we find a view that wants the mouse event.
344 for (mouse_pressed_handler_ = GetEventHandlerForPoint(event.location());
345 mouse_pressed_handler_ && (mouse_pressed_handler_ != this);
346 mouse_pressed_handler_ = mouse_pressed_handler_->parent()) {
347 DVLOG(1) << "OnMousePressed testing "
348 << mouse_pressed_handler_->GetClassName();
349 if (!mouse_pressed_handler_->enabled()) {
350 // Disabled views should eat events instead of propagating them upwards.
351 hit_disabled_view = true;
352 break;
355 // See if this view wants to handle the mouse press.
356 ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
357 mouse_pressed_handler_);
359 // Remove the double-click flag if the handler is different than the
360 // one which got the first click part of the double-click.
361 if (mouse_pressed_handler_ != last_click_handler_)
362 mouse_pressed_event.set_flags(event.flags() & ~ui::EF_IS_DOUBLE_CLICK);
364 drag_info_.Reset();
365 ui::EventDispatchDetails dispatch_details =
366 DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event);
367 if (dispatch_details.dispatcher_destroyed)
368 return mouse_pressed_event.handled();
370 // The view could have removed itself from the tree when handling
371 // OnMousePressed(). In this case, the removal notification will have
372 // reset mouse_pressed_handler_ to NULL out from under us. Detect this
373 // case and stop. (See comments in view.h.)
375 // NOTE: Don't return true here, because we don't want the frame to
376 // forward future events to us when there's no handler.
377 if (!mouse_pressed_handler_)
378 break;
380 // If the view handled the event, leave mouse_pressed_handler_ set and
381 // return true, which will cause subsequent drag/release events to get
382 // forwarded to that view.
383 if (mouse_pressed_event.handled()) {
384 last_click_handler_ = mouse_pressed_handler_;
385 DVLOG(1) << "OnMousePressed handled by "
386 << mouse_pressed_handler_->GetClassName();
387 return true;
391 // Reset mouse_pressed_handler_ to indicate that no processing is occurring.
392 mouse_pressed_handler_ = NULL;
394 // In the event that a double-click is not handled after traversing the
395 // entire hierarchy (even as a single-click when sent to a different view),
396 // it must be marked as handled to avoid anything happening from default
397 // processing if it the first click-part was handled by us.
398 if (last_click_handler_ && (event.flags() & ui::EF_IS_DOUBLE_CLICK))
399 hit_disabled_view = true;
401 last_click_handler_ = NULL;
402 return hit_disabled_view;
405 bool RootView::OnMouseDragged(const ui::MouseEvent& event) {
406 if (mouse_pressed_handler_) {
407 SetMouseLocationAndFlags(event);
409 ui::MouseEvent mouse_event(event, static_cast<View*>(this),
410 mouse_pressed_handler_);
411 ui::EventDispatchDetails dispatch_details =
412 DispatchEvent(mouse_pressed_handler_, &mouse_event);
413 if (dispatch_details.dispatcher_destroyed)
414 return false;
416 return false;
419 void RootView::OnMouseReleased(const ui::MouseEvent& event) {
420 UpdateCursor(event);
422 if (mouse_pressed_handler_) {
423 ui::MouseEvent mouse_released(event, static_cast<View*>(this),
424 mouse_pressed_handler_);
425 // We allow the view to delete us from the event dispatch callback. As such,
426 // configure state such that we're done first, then call View.
427 View* mouse_pressed_handler = mouse_pressed_handler_;
428 SetMouseHandler(NULL);
429 ui::EventDispatchDetails dispatch_details =
430 DispatchEvent(mouse_pressed_handler, &mouse_released);
431 if (dispatch_details.dispatcher_destroyed)
432 return;
436 void RootView::OnMouseCaptureLost() {
437 // TODO: this likely needs to reset touch handler too.
439 if (mouse_pressed_handler_ || gesture_handler_) {
440 // Synthesize a release event for UpdateCursor.
441 if (mouse_pressed_handler_) {
442 gfx::Point last_point(last_mouse_event_x_, last_mouse_event_y_);
443 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
444 last_point, last_point,
445 last_mouse_event_flags_,
447 UpdateCursor(release_event);
449 // We allow the view to delete us from OnMouseCaptureLost. As such,
450 // configure state such that we're done first, then call View.
451 View* mouse_pressed_handler = mouse_pressed_handler_;
452 View* gesture_handler = gesture_handler_;
453 SetMouseHandler(NULL);
454 if (mouse_pressed_handler)
455 mouse_pressed_handler->OnMouseCaptureLost();
456 else
457 gesture_handler->OnMouseCaptureLost();
458 // WARNING: we may have been deleted.
462 void RootView::OnMouseMoved(const ui::MouseEvent& event) {
463 View* v = GetEventHandlerForPoint(event.location());
464 // Find the first enabled view, or the existing move handler, whichever comes
465 // first. The check for the existing handler is because if a view becomes
466 // disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED
467 // and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet.
468 while (v && !v->enabled() && (v != mouse_move_handler_))
469 v = v->parent();
470 if (v && v != this) {
471 if (v != mouse_move_handler_) {
472 if (mouse_move_handler_ != NULL &&
473 (!mouse_move_handler_->notify_enter_exit_on_child() ||
474 !mouse_move_handler_->Contains(v))) {
475 MouseEnterExitEvent exit(event, ui::ET_MOUSE_EXITED);
476 exit.ConvertLocationToTarget(static_cast<View*>(this),
477 mouse_move_handler_);
478 ui::EventDispatchDetails dispatch_details =
479 DispatchEvent(mouse_move_handler_, &exit);
480 if (dispatch_details.dispatcher_destroyed)
481 return;
482 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
483 mouse_move_handler_, v);
485 View* old_handler = mouse_move_handler_;
486 mouse_move_handler_ = v;
487 if (!mouse_move_handler_->notify_enter_exit_on_child() ||
488 !mouse_move_handler_->Contains(old_handler)) {
489 MouseEnterExitEvent entered(event, ui::ET_MOUSE_ENTERED);
490 entered.ConvertLocationToTarget(static_cast<View*>(this),
491 mouse_move_handler_);
492 ui::EventDispatchDetails dispatch_details =
493 DispatchEvent(mouse_move_handler_, &entered);
494 if (dispatch_details.dispatcher_destroyed)
495 return;
496 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_ENTERED,
497 mouse_move_handler_, old_handler);
500 ui::MouseEvent moved_event(event, static_cast<View*>(this),
501 mouse_move_handler_);
502 mouse_move_handler_->OnMouseMoved(moved_event);
503 // TODO(tdanderson): It may be possible to avoid setting the cursor twice
504 // (once here and once from CompoundEventFilter) on a
505 // mousemove. See crbug.com/351469.
506 if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT))
507 widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event));
508 } else if (mouse_move_handler_ != NULL) {
509 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
510 ui::EventDispatchDetails dispatch_details =
511 DispatchEvent(mouse_move_handler_, &exited);
512 if (dispatch_details.dispatcher_destroyed)
513 return;
514 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
515 mouse_move_handler_, v);
516 // On Aura the non-client area extends slightly outside the root view for
517 // some windows. Let the non-client cursor handling code set the cursor
518 // as we do above.
519 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
520 widget_->SetCursor(gfx::kNullCursor);
521 mouse_move_handler_ = NULL;
525 void RootView::OnMouseExited(const ui::MouseEvent& event) {
526 if (mouse_move_handler_ != NULL) {
527 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
528 ui::EventDispatchDetails dispatch_details =
529 DispatchEvent(mouse_move_handler_, &exited);
530 if (dispatch_details.dispatcher_destroyed)
531 return;
532 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
533 mouse_move_handler_, NULL);
534 mouse_move_handler_ = NULL;
538 bool RootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
539 for (View* v = GetEventHandlerForPoint(event.location());
540 v && v != this && !event.handled(); v = v->parent()) {
541 ui::EventDispatchDetails dispatch_details =
542 DispatchEvent(v, const_cast<ui::MouseWheelEvent*>(&event));
543 if (dispatch_details.dispatcher_destroyed ||
544 dispatch_details.target_destroyed) {
545 return event.handled();
548 return event.handled();
551 void RootView::SetMouseHandler(View* new_mh) {
552 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well.
553 explicit_mouse_handler_ = (new_mh != NULL);
554 mouse_pressed_handler_ = new_mh;
555 gesture_handler_ = new_mh;
556 scroll_gesture_handler_ = new_mh;
557 drag_info_.Reset();
560 void RootView::GetAccessibleState(ui::AXViewState* state) {
561 state->name = widget_->widget_delegate()->GetAccessibleWindowTitle();
562 state->role = widget_->widget_delegate()->GetAccessibleWindowRole();
565 void RootView::UpdateParentLayer() {
566 if (layer())
567 ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer());
570 ////////////////////////////////////////////////////////////////////////////////
571 // RootView, protected:
573 void RootView::ViewHierarchyChanged(
574 const ViewHierarchyChangedDetails& details) {
575 widget_->ViewHierarchyChanged(details);
577 if (!details.is_add) {
578 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child)
579 mouse_pressed_handler_ = NULL;
580 if (mouse_move_handler_ == details.child)
581 mouse_move_handler_ = NULL;
582 if (gesture_handler_ == details.child)
583 gesture_handler_ = NULL;
584 if (scroll_gesture_handler_ == details.child)
585 scroll_gesture_handler_ = NULL;
586 if (event_dispatch_target_ == details.child)
587 event_dispatch_target_ = NULL;
588 if (old_dispatch_target_ == details.child)
589 old_dispatch_target_ = NULL;
593 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) {
594 if (!is_visible) {
595 // When the root view is being hidden (e.g. when widget is minimized)
596 // handlers are reset, so that after it is reshown, events are not captured
597 // by old handlers.
598 explicit_mouse_handler_ = false;
599 mouse_pressed_handler_ = NULL;
600 mouse_move_handler_ = NULL;
601 gesture_handler_ = NULL;
602 scroll_gesture_handler_ = NULL;
603 event_dispatch_target_ = NULL;
604 old_dispatch_target_ = NULL;
608 void RootView::OnPaint(gfx::Canvas* canvas) {
609 if (!layer() || !layer()->fills_bounds_opaquely())
610 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
612 View::OnPaint(canvas);
615 gfx::Vector2d RootView::CalculateOffsetToAncestorWithLayer(
616 ui::Layer** layer_parent) {
617 gfx::Vector2d offset(View::CalculateOffsetToAncestorWithLayer(layer_parent));
618 if (!layer() && layer_parent)
619 *layer_parent = widget_->GetLayer();
620 return offset;
623 View::DragInfo* RootView::GetDragInfo() {
624 return &drag_info_;
627 ////////////////////////////////////////////////////////////////////////////////
628 // RootView, private:
630 // Input -----------------------------------------------------------------------
632 void RootView::DispatchGestureEvent(ui::GestureEvent* event) {
633 if (gesture_handler_) {
634 // |gesture_handler_| (or |scroll_gesture_handler_|) can be deleted during
635 // processing.
636 View* handler = scroll_gesture_handler_ &&
637 (event->IsScrollGestureEvent() || event->IsFlingScrollEvent()) ?
638 scroll_gesture_handler_ : gesture_handler_;
639 ui::GestureEvent handler_event(*event, static_cast<View*>(this), handler);
640 ui::EventDispatchDetails dispatch_details =
641 DispatchEvent(handler, &handler_event);
642 if (dispatch_details.dispatcher_destroyed)
643 return;
645 if (event->type() == ui::ET_GESTURE_END &&
646 event->details().touch_points() <= 1) {
647 // In case a drag was in progress, reset all the handlers. Otherwise, just
648 // reset the gesture handler.
649 if (gesture_handler_ == mouse_pressed_handler_)
650 SetMouseHandler(NULL);
651 else
652 gesture_handler_ = NULL;
655 if (scroll_gesture_handler_ &&
656 (event->type() == ui::ET_GESTURE_SCROLL_END ||
657 event->type() == ui::ET_SCROLL_FLING_START)) {
658 scroll_gesture_handler_ = NULL;
661 if (handler_event.stopped_propagation()) {
662 event->StopPropagation();
663 return;
664 } else if (handler_event.handled()) {
665 event->SetHandled();
666 return;
669 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN &&
670 !scroll_gesture_handler_) {
671 // Some view started processing gesture events, however it does not
672 // process scroll-gesture events. In such case, we allow the event to
673 // bubble up, and install a different scroll-gesture handler different
674 // from the default gesture handler.
675 for (scroll_gesture_handler_ = gesture_handler_->parent();
676 scroll_gesture_handler_ && scroll_gesture_handler_ != this;
677 scroll_gesture_handler_ = scroll_gesture_handler_->parent()) {
678 ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
679 scroll_gesture_handler_);
680 ui::EventDispatchDetails dispatch_details =
681 DispatchEvent(scroll_gesture_handler_, &gesture_event);
682 if (gesture_event.stopped_propagation()) {
683 event->StopPropagation();
684 return;
685 } else if (gesture_event.handled()) {
686 event->SetHandled();
687 return;
688 } else if (dispatch_details.dispatcher_destroyed ||
689 dispatch_details.target_destroyed) {
690 return;
693 scroll_gesture_handler_ = NULL;
696 return;
699 View* gesture_handler = NULL;
700 if (views::switches::IsRectBasedTargetingEnabled() &&
701 !event->details().bounding_box().IsEmpty()) {
702 // TODO(tdanderson): Pass in the bounding box to GetEventHandlerForRect()
703 // once crbug.com/313392 is resolved.
704 gfx::Rect touch_rect(event->details().bounding_box());
705 touch_rect.set_origin(event->location());
706 touch_rect.Offset(-touch_rect.width() / 2, -touch_rect.height() / 2);
707 gesture_handler = GetEventHandlerForRect(touch_rect);
708 } else {
709 gesture_handler = GetEventHandlerForPoint(event->location());
712 // Walk up the tree until we find a view that wants the gesture event.
713 for (gesture_handler_ = gesture_handler;
714 gesture_handler_ && (gesture_handler_ != this);
715 gesture_handler_ = gesture_handler_->parent()) {
716 if (!gesture_handler_->enabled()) {
717 // Disabled views eat events but are treated as not handled.
718 return;
721 // See if this view wants to handle the Gesture.
722 ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
723 gesture_handler_);
724 ui::EventDispatchDetails dispatch_details =
725 DispatchEvent(gesture_handler_, &gesture_event);
726 if (dispatch_details.dispatcher_destroyed)
727 return;
729 // The view could have removed itself from the tree when handling
730 // OnGestureEvent(). So handle as per OnMousePressed. NB: we
731 // assume that the RootView itself cannot be so removed.
732 if (!gesture_handler_)
733 return;
735 if (gesture_event.handled()) {
736 if (gesture_event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
737 scroll_gesture_handler_ = gesture_handler_;
738 if (gesture_event.stopped_propagation())
739 event->StopPropagation();
740 else
741 event->SetHandled();
742 // Last ui::ET_GESTURE_END should not set the gesture_handler_.
743 if (gesture_event.type() == ui::ET_GESTURE_END &&
744 event->details().touch_points() <= 1) {
745 gesture_handler_ = NULL;
747 return;
750 // The gesture event wasn't processed. Go up the view hierarchy and
751 // dispatch the gesture event.
754 gesture_handler_ = NULL;
757 void RootView::UpdateCursor(const ui::MouseEvent& event) {
758 if (!(event.flags() & ui::EF_IS_NON_CLIENT)) {
759 View* v = GetEventHandlerForPoint(event.location());
760 ui::MouseEvent me(event, static_cast<View*>(this), v);
761 widget_->SetCursor(v->GetCursor(me));
765 void RootView::SetMouseLocationAndFlags(const ui::MouseEvent& event) {
766 last_mouse_event_flags_ = event.flags();
767 last_mouse_event_x_ = event.x();
768 last_mouse_event_y_ = event.y();
771 void RootView::NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
772 ui::EventType type,
773 View* view,
774 View* sibling) {
775 for (View* p = view->parent(); p; p = p->parent()) {
776 if (!p->notify_enter_exit_on_child())
777 continue;
778 if (sibling && p->Contains(sibling))
779 break;
780 // It is necessary to recreate the notify-event for each dispatch, since one
781 // of the callbacks can mark the event as handled, and that would cause
782 // incorrect event dispatch.
783 MouseEnterExitEvent notify_event(event, type);
784 ui::EventDispatchDetails dispatch_details = DispatchEvent(p, &notify_event);
785 if (dispatch_details.dispatcher_destroyed ||
786 dispatch_details.target_destroyed) {
787 return;
792 bool RootView::CanDispatchToTarget(ui::EventTarget* target) {
793 return event_dispatch_target_ == target;
796 ui::EventDispatchDetails RootView::PreDispatchEvent(ui::EventTarget* target,
797 ui::Event* event) {
798 old_dispatch_target_ = event_dispatch_target_;
799 event_dispatch_target_ = static_cast<View*>(target);
800 return DispatchDetails();
803 ui::EventDispatchDetails RootView::PostDispatchEvent(ui::EventTarget* target,
804 const ui::Event& event) {
805 DispatchDetails details;
806 if (target != event_dispatch_target_)
807 details.target_destroyed = true;
809 event_dispatch_target_ = old_dispatch_target_;
810 old_dispatch_target_ = NULL;
812 #ifndef NDEBUG
813 DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_));
814 #endif
816 return details;
819 } // namespace internal
820 } // namespace views