Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / widget / root_view.cc
bloba546ad5206e5fb7af1c7fc6fedc94b73d1cb67ef
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/widget/root_view_targeter.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 allow_gesture_event_retargeting_(true),
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 RootViewTargeter(this, 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 ui::GestureEvent* gesture_event = event->AsGestureEvent();
268 // Do not dispatch ui::ET_GESTURE_BEGIN events.
269 if (gesture_event->type() == ui::ET_GESTURE_BEGIN)
270 return DispatchDetails();
272 // Ignore ui::ET_GESTURE_END events which do not correspond to the
273 // removal of the final touch point.
274 if (gesture_event->type() == ui::ET_GESTURE_END &&
275 gesture_event->details().touch_points() > 1) {
276 return DispatchDetails();
279 // Ignore subsequent gesture scroll events if no handler was set for a
280 // ui::ET_GESTURE_SCROLL_BEGIN event.
281 if (!gesture_handler_ &&
282 (gesture_event->type() == ui::ET_GESTURE_SCROLL_UPDATE ||
283 gesture_event->type() == ui::ET_GESTURE_SCROLL_END ||
284 gesture_event->type() == ui::ET_SCROLL_FLING_START)) {
285 return DispatchDetails();
288 // If |gesture_handler_| is non-null (as a result of dispatching a previous
289 // gesture event), then |gesture_event| should be dispatched only to
290 // |gesture_handler_|.
291 allow_gesture_event_retargeting_ = gesture_handler_ ? false : true;
293 DispatchGestureEvent(gesture_event);
294 return DispatchDetails();
297 if (event->IsTouchEvent())
298 NOTREACHED() << "Touch events should not be sent to RootView.";
300 if (event->IsMouseEvent())
301 NOTREACHED() << "Should not be called with a MouseEvent.";
303 return DispatchDetails();
306 ////////////////////////////////////////////////////////////////////////////////
307 // RootView, View overrides:
309 const Widget* RootView::GetWidget() const {
310 return widget_;
313 Widget* RootView::GetWidget() {
314 return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget());
317 bool RootView::IsDrawn() const {
318 return visible();
321 void RootView::Layout() {
322 View::Layout();
323 widget_->OnRootViewLayout();
326 const char* RootView::GetClassName() const {
327 return kViewClassName;
330 void RootView::SchedulePaintInRect(const gfx::Rect& rect) {
331 if (layer()) {
332 layer()->SchedulePaint(rect);
333 } else {
334 gfx::Rect xrect = ConvertRectToParent(rect);
335 gfx::Rect invalid_rect = gfx::IntersectRects(GetLocalBounds(), xrect);
336 if (!invalid_rect.IsEmpty())
337 widget_->SchedulePaintInRect(invalid_rect);
341 bool RootView::OnMousePressed(const ui::MouseEvent& event) {
342 UpdateCursor(event);
343 SetMouseLocationAndFlags(event);
345 // If mouse_pressed_handler_ is non null, we are currently processing
346 // a pressed -> drag -> released session. In that case we send the
347 // event to mouse_pressed_handler_
348 if (mouse_pressed_handler_) {
349 ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
350 mouse_pressed_handler_);
351 drag_info_.Reset();
352 ui::EventDispatchDetails dispatch_details =
353 DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event);
354 if (dispatch_details.dispatcher_destroyed)
355 return true;
356 return true;
358 DCHECK(!explicit_mouse_handler_);
360 bool hit_disabled_view = false;
361 // Walk up the tree until we find a view that wants the mouse event.
362 for (mouse_pressed_handler_ = GetEventHandlerForPoint(event.location());
363 mouse_pressed_handler_ && (mouse_pressed_handler_ != this);
364 mouse_pressed_handler_ = mouse_pressed_handler_->parent()) {
365 DVLOG(1) << "OnMousePressed testing "
366 << mouse_pressed_handler_->GetClassName();
367 if (!mouse_pressed_handler_->enabled()) {
368 // Disabled views should eat events instead of propagating them upwards.
369 hit_disabled_view = true;
370 break;
373 // See if this view wants to handle the mouse press.
374 ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
375 mouse_pressed_handler_);
377 // Remove the double-click flag if the handler is different than the
378 // one which got the first click part of the double-click.
379 if (mouse_pressed_handler_ != last_click_handler_)
380 mouse_pressed_event.set_flags(event.flags() & ~ui::EF_IS_DOUBLE_CLICK);
382 drag_info_.Reset();
383 ui::EventDispatchDetails dispatch_details =
384 DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event);
385 if (dispatch_details.dispatcher_destroyed)
386 return mouse_pressed_event.handled();
388 // The view could have removed itself from the tree when handling
389 // OnMousePressed(). In this case, the removal notification will have
390 // reset mouse_pressed_handler_ to NULL out from under us. Detect this
391 // case and stop. (See comments in view.h.)
393 // NOTE: Don't return true here, because we don't want the frame to
394 // forward future events to us when there's no handler.
395 if (!mouse_pressed_handler_)
396 break;
398 // If the view handled the event, leave mouse_pressed_handler_ set and
399 // return true, which will cause subsequent drag/release events to get
400 // forwarded to that view.
401 if (mouse_pressed_event.handled()) {
402 last_click_handler_ = mouse_pressed_handler_;
403 DVLOG(1) << "OnMousePressed handled by "
404 << mouse_pressed_handler_->GetClassName();
405 return true;
409 // Reset mouse_pressed_handler_ to indicate that no processing is occurring.
410 mouse_pressed_handler_ = NULL;
412 // In the event that a double-click is not handled after traversing the
413 // entire hierarchy (even as a single-click when sent to a different view),
414 // it must be marked as handled to avoid anything happening from default
415 // processing if it the first click-part was handled by us.
416 if (last_click_handler_ && (event.flags() & ui::EF_IS_DOUBLE_CLICK))
417 hit_disabled_view = true;
419 last_click_handler_ = NULL;
420 return hit_disabled_view;
423 bool RootView::OnMouseDragged(const ui::MouseEvent& event) {
424 if (mouse_pressed_handler_) {
425 SetMouseLocationAndFlags(event);
427 ui::MouseEvent mouse_event(event, static_cast<View*>(this),
428 mouse_pressed_handler_);
429 ui::EventDispatchDetails dispatch_details =
430 DispatchEvent(mouse_pressed_handler_, &mouse_event);
431 if (dispatch_details.dispatcher_destroyed)
432 return false;
434 return false;
437 void RootView::OnMouseReleased(const ui::MouseEvent& event) {
438 UpdateCursor(event);
440 if (mouse_pressed_handler_) {
441 ui::MouseEvent mouse_released(event, static_cast<View*>(this),
442 mouse_pressed_handler_);
443 // We allow the view to delete us from the event dispatch callback. As such,
444 // configure state such that we're done first, then call View.
445 View* mouse_pressed_handler = mouse_pressed_handler_;
446 SetMouseHandler(NULL);
447 ui::EventDispatchDetails dispatch_details =
448 DispatchEvent(mouse_pressed_handler, &mouse_released);
449 if (dispatch_details.dispatcher_destroyed)
450 return;
454 void RootView::OnMouseCaptureLost() {
455 // TODO: this likely needs to reset touch handler too.
457 if (mouse_pressed_handler_ || gesture_handler_) {
458 // Synthesize a release event for UpdateCursor.
459 if (mouse_pressed_handler_) {
460 gfx::Point last_point(last_mouse_event_x_, last_mouse_event_y_);
461 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
462 last_point, last_point,
463 last_mouse_event_flags_,
465 UpdateCursor(release_event);
467 // We allow the view to delete us from OnMouseCaptureLost. As such,
468 // configure state such that we're done first, then call View.
469 View* mouse_pressed_handler = mouse_pressed_handler_;
470 View* gesture_handler = gesture_handler_;
471 SetMouseHandler(NULL);
472 if (mouse_pressed_handler)
473 mouse_pressed_handler->OnMouseCaptureLost();
474 else
475 gesture_handler->OnMouseCaptureLost();
476 // WARNING: we may have been deleted.
480 void RootView::OnMouseMoved(const ui::MouseEvent& event) {
481 View* v = GetEventHandlerForPoint(event.location());
482 // Find the first enabled view, or the existing move handler, whichever comes
483 // first. The check for the existing handler is because if a view becomes
484 // disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED
485 // and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet.
486 while (v && !v->enabled() && (v != mouse_move_handler_))
487 v = v->parent();
488 if (v && v != this) {
489 if (v != mouse_move_handler_) {
490 if (mouse_move_handler_ != NULL &&
491 (!mouse_move_handler_->notify_enter_exit_on_child() ||
492 !mouse_move_handler_->Contains(v))) {
493 MouseEnterExitEvent exit(event, ui::ET_MOUSE_EXITED);
494 exit.ConvertLocationToTarget(static_cast<View*>(this),
495 mouse_move_handler_);
496 ui::EventDispatchDetails dispatch_details =
497 DispatchEvent(mouse_move_handler_, &exit);
498 if (dispatch_details.dispatcher_destroyed)
499 return;
500 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
501 mouse_move_handler_, v);
503 View* old_handler = mouse_move_handler_;
504 mouse_move_handler_ = v;
505 if (!mouse_move_handler_->notify_enter_exit_on_child() ||
506 !mouse_move_handler_->Contains(old_handler)) {
507 MouseEnterExitEvent entered(event, ui::ET_MOUSE_ENTERED);
508 entered.ConvertLocationToTarget(static_cast<View*>(this),
509 mouse_move_handler_);
510 ui::EventDispatchDetails dispatch_details =
511 DispatchEvent(mouse_move_handler_, &entered);
512 if (dispatch_details.dispatcher_destroyed)
513 return;
514 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_ENTERED,
515 mouse_move_handler_, old_handler);
518 ui::MouseEvent moved_event(event, static_cast<View*>(this),
519 mouse_move_handler_);
520 mouse_move_handler_->OnMouseMoved(moved_event);
521 // TODO(tdanderson): It may be possible to avoid setting the cursor twice
522 // (once here and once from CompoundEventFilter) on a
523 // mousemove. See crbug.com/351469.
524 if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT))
525 widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event));
526 } else 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_, v);
534 // On Aura the non-client area extends slightly outside the root view for
535 // some windows. Let the non-client cursor handling code set the cursor
536 // as we do above.
537 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
538 widget_->SetCursor(gfx::kNullCursor);
539 mouse_move_handler_ = NULL;
543 void RootView::OnMouseExited(const ui::MouseEvent& event) {
544 if (mouse_move_handler_ != NULL) {
545 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
546 ui::EventDispatchDetails dispatch_details =
547 DispatchEvent(mouse_move_handler_, &exited);
548 if (dispatch_details.dispatcher_destroyed)
549 return;
550 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
551 mouse_move_handler_, NULL);
552 mouse_move_handler_ = NULL;
556 bool RootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
557 for (View* v = GetEventHandlerForPoint(event.location());
558 v && v != this && !event.handled(); v = v->parent()) {
559 ui::EventDispatchDetails dispatch_details =
560 DispatchEvent(v, const_cast<ui::MouseWheelEvent*>(&event));
561 if (dispatch_details.dispatcher_destroyed ||
562 dispatch_details.target_destroyed) {
563 return event.handled();
566 return event.handled();
569 void RootView::SetMouseHandler(View* new_mh) {
570 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well.
571 explicit_mouse_handler_ = (new_mh != NULL);
572 mouse_pressed_handler_ = new_mh;
573 gesture_handler_ = new_mh;
574 drag_info_.Reset();
577 void RootView::GetAccessibleState(ui::AXViewState* state) {
578 state->name = widget_->widget_delegate()->GetAccessibleWindowTitle();
579 state->role = widget_->widget_delegate()->GetAccessibleWindowRole();
582 void RootView::UpdateParentLayer() {
583 if (layer())
584 ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer());
587 ////////////////////////////////////////////////////////////////////////////////
588 // RootView, protected:
590 void RootView::ViewHierarchyChanged(
591 const ViewHierarchyChangedDetails& details) {
592 widget_->ViewHierarchyChanged(details);
594 if (!details.is_add) {
595 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child)
596 mouse_pressed_handler_ = NULL;
597 if (mouse_move_handler_ == details.child)
598 mouse_move_handler_ = NULL;
599 if (gesture_handler_ == details.child)
600 gesture_handler_ = NULL;
601 if (event_dispatch_target_ == details.child)
602 event_dispatch_target_ = NULL;
603 if (old_dispatch_target_ == details.child)
604 old_dispatch_target_ = NULL;
608 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) {
609 if (!is_visible) {
610 // When the root view is being hidden (e.g. when widget is minimized)
611 // handlers are reset, so that after it is reshown, events are not captured
612 // by old handlers.
613 explicit_mouse_handler_ = false;
614 mouse_pressed_handler_ = NULL;
615 mouse_move_handler_ = NULL;
616 gesture_handler_ = NULL;
617 event_dispatch_target_ = NULL;
618 old_dispatch_target_ = NULL;
622 void RootView::OnPaint(gfx::Canvas* canvas) {
623 if (!layer() || !layer()->fills_bounds_opaquely())
624 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
626 View::OnPaint(canvas);
629 gfx::Vector2d RootView::CalculateOffsetToAncestorWithLayer(
630 ui::Layer** layer_parent) {
631 gfx::Vector2d offset(View::CalculateOffsetToAncestorWithLayer(layer_parent));
632 if (!layer() && layer_parent)
633 *layer_parent = widget_->GetLayer();
634 return offset;
637 View::DragInfo* RootView::GetDragInfo() {
638 return &drag_info_;
641 ////////////////////////////////////////////////////////////////////////////////
642 // RootView, private:
644 // Input -----------------------------------------------------------------------
646 void RootView::DispatchGestureEvent(ui::GestureEvent* event) {
647 if (gesture_handler_) {
648 if (gesture_handler_->enabled()) {
649 // |gesture_handler_| can be deleted during processing. In particular, it
650 // will be set to NULL if the view is deleted or removed from the tree as
651 // a result of an event dispatch.
652 ui::GestureEvent handler_event(*event,
653 static_cast<View*>(this),
654 gesture_handler_);
655 ui::EventDispatchDetails dispatch_details =
656 DispatchEvent(gesture_handler_, &handler_event);
657 if (dispatch_details.dispatcher_destroyed)
658 return;
660 if (handler_event.stopped_propagation())
661 event->StopPropagation();
662 else if (handler_event.handled())
663 event->SetHandled();
664 } else {
665 // Disabled views are permitted to be targets of gesture events, but
666 // gesture events should never actually be dispatched to them.
667 event->SetHandled();
670 if (event->type() == ui::ET_GESTURE_END) {
671 DCHECK_EQ(1, event->details().touch_points());
672 // In case a drag was in progress, reset all the handlers. Otherwise, just
673 // reset the gesture handler.
674 if (gesture_handler_ == mouse_pressed_handler_)
675 SetMouseHandler(NULL);
676 else
677 gesture_handler_ = NULL;
680 if (event->handled())
681 return;
683 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
684 // Some view started processing gesture events, however it does not
685 // process scroll-gesture events. In such case, we allow the event to
686 // bubble up. |gesture_handler_| is changed to its nearest ancestor
687 // that handles scroll-gesture events.
688 gesture_handler_ = static_cast<View*>(
689 targeter()->FindNextBestTarget(gesture_handler_, event));
690 while (gesture_handler_ && gesture_handler_ != this) {
691 ui::GestureEvent gesture_event(*event,
692 static_cast<View*>(this),
693 gesture_handler_);
694 ui::EventDispatchDetails dispatch_details =
695 DispatchEvent(gesture_handler_, &gesture_event);
696 if (gesture_event.stopped_propagation()) {
697 event->StopPropagation();
698 return;
699 } else if (gesture_event.handled()) {
700 event->SetHandled();
701 return;
702 } else if (dispatch_details.dispatcher_destroyed ||
703 dispatch_details.target_destroyed) {
704 return;
706 gesture_handler_ = static_cast<View*>(
707 targeter()->FindNextBestTarget(gesture_handler_, event));
709 gesture_handler_ = NULL;
712 return;
715 // Walk up the tree until we find a view that wants the gesture event.
716 gesture_handler_ =
717 static_cast<View*>(targeter()->FindTargetForEvent(this, event));
718 while (gesture_handler_ && gesture_handler_ != this) {
719 // Disabled views are permitted to be targets of gesture events, but
720 // gesture events should never actually be dispatched to them.
721 if (!gesture_handler_->enabled()) {
722 event->SetHandled();
724 // Last ui::ET_GESTURE_END should not set the gesture_handler_.
725 if (event->type() == ui::ET_GESTURE_END) {
726 DCHECK_EQ(1, event->details().touch_points());
727 gesture_handler_ = NULL;
730 return;
733 // See if this view wants to handle the Gesture.
734 ui::GestureEvent gesture_event(*event,
735 static_cast<View*>(this),
736 gesture_handler_);
737 ui::EventDispatchDetails dispatch_details =
738 DispatchEvent(gesture_handler_, &gesture_event);
739 if (dispatch_details.dispatcher_destroyed)
740 return;
742 // The view could have removed itself from the tree when handling
743 // OnGestureEvent(). So handle as per OnMousePressed. NB: we
744 // assume that the RootView itself cannot be so removed.
745 if (!gesture_handler_)
746 return;
748 if (gesture_event.handled()) {
749 if (gesture_event.stopped_propagation())
750 event->StopPropagation();
751 else
752 event->SetHandled();
753 // Last ui::ET_GESTURE_END should not set the gesture_handler_.
754 if (gesture_event.type() == ui::ET_GESTURE_END) {
755 DCHECK_EQ(1, event->details().touch_points());
756 gesture_handler_ = NULL;
758 return;
761 // The gesture event wasn't processed. Go up the view hierarchy and
762 // dispatch the gesture event.
763 gesture_handler_ = static_cast<View*>(
764 targeter()->FindNextBestTarget(gesture_handler_, event));
767 gesture_handler_ = NULL;
770 void RootView::UpdateCursor(const ui::MouseEvent& event) {
771 if (!(event.flags() & ui::EF_IS_NON_CLIENT)) {
772 View* v = GetEventHandlerForPoint(event.location());
773 ui::MouseEvent me(event, static_cast<View*>(this), v);
774 widget_->SetCursor(v->GetCursor(me));
778 void RootView::SetMouseLocationAndFlags(const ui::MouseEvent& event) {
779 last_mouse_event_flags_ = event.flags();
780 last_mouse_event_x_ = event.x();
781 last_mouse_event_y_ = event.y();
784 void RootView::NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
785 ui::EventType type,
786 View* view,
787 View* sibling) {
788 for (View* p = view->parent(); p; p = p->parent()) {
789 if (!p->notify_enter_exit_on_child())
790 continue;
791 if (sibling && p->Contains(sibling))
792 break;
793 // It is necessary to recreate the notify-event for each dispatch, since one
794 // of the callbacks can mark the event as handled, and that would cause
795 // incorrect event dispatch.
796 MouseEnterExitEvent notify_event(event, type);
797 ui::EventDispatchDetails dispatch_details = DispatchEvent(p, &notify_event);
798 if (dispatch_details.dispatcher_destroyed ||
799 dispatch_details.target_destroyed) {
800 return;
805 bool RootView::CanDispatchToTarget(ui::EventTarget* target) {
806 return event_dispatch_target_ == target;
809 ui::EventDispatchDetails RootView::PreDispatchEvent(ui::EventTarget* target,
810 ui::Event* event) {
811 old_dispatch_target_ = event_dispatch_target_;
812 event_dispatch_target_ = static_cast<View*>(target);
813 return DispatchDetails();
816 ui::EventDispatchDetails RootView::PostDispatchEvent(ui::EventTarget* target,
817 const ui::Event& event) {
818 DispatchDetails details;
819 if (target != event_dispatch_target_)
820 details.target_destroyed = true;
822 event_dispatch_target_ = old_dispatch_target_;
823 old_dispatch_target_ = NULL;
825 #ifndef NDEBUG
826 DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_));
827 #endif
829 return details;
832 } // namespace internal
833 } // namespace views