This source file includes following definitions.
- OnKeyEvent
- old_dispatch_target_
- SetContentsView
- GetContentsView
- NotifyNativeViewHierarchyChanged
- SetFocusTraversableParent
- SetFocusTraversableParentView
- ThemeChanged
- LocaleChanged
- GetFocusSearch
- GetFocusTraversableParent
- GetFocusTraversableParentView
- GetRootTarget
- OnEventFromSource
- GetWidget
- GetWidget
- IsDrawn
- Layout
- GetClassName
- SchedulePaintInRect
- OnMousePressed
- OnMouseDragged
- OnMouseReleased
- OnMouseCaptureLost
- OnMouseMoved
- OnMouseExited
- OnMouseWheel
- SetMouseHandler
- GetAccessibleState
- UpdateParentLayer
- DispatchGestureEvent
- ViewHierarchyChanged
- VisibilityChanged
- OnPaint
- CalculateOffsetToAncestorWithLayer
- GetDragInfo
- DispatchScrollEvent
- UpdateCursor
- SetMouseLocationAndFlags
- NotifyEnterExitOfDescendant
- CanDispatchToTarget
- PreDispatchEvent
- PostDispatchEvent
#include "ui/views/widget/root_view.h"
#include <algorithm>
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/compositor/layer.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas.h"
#include "ui/views/focus/view_storage.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view_targeter.h"
#include "ui/views/views_switches.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
typedef ui::EventDispatchDetails DispatchDetails;
namespace views {
namespace internal {
namespace {
enum EventType {
EVENT_ENTER,
EVENT_EXIT
};
class MouseEnterExitEvent : public ui::MouseEvent {
public:
MouseEnterExitEvent(const ui::MouseEvent& event, ui::EventType type)
: ui::MouseEvent(event,
static_cast<View*>(NULL),
static_cast<View*>(NULL)) {
DCHECK(type == ui::ET_MOUSE_ENTERED ||
type == ui::ET_MOUSE_EXITED);
SetType(type);
}
virtual ~MouseEnterExitEvent() {}
};
}
class PreEventDispatchHandler : public ui::EventHandler {
public:
explicit PreEventDispatchHandler(View* owner)
: owner_(owner) {
}
virtual ~PreEventDispatchHandler() {}
private:
virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
CHECK_EQ(ui::EP_PRETARGET, event->phase());
if (event->handled())
return;
View* v = NULL;
if (owner_->GetFocusManager())
v = owner_->GetFocusManager()->GetFocusedView();
if (v && v->enabled() && ((event->key_code() == ui::VKEY_APPS) ||
(event->key_code() == ui::VKEY_F10 && event->IsShiftDown()))) {
gfx::Point location = v->GetKeyboardContextMenuLocation();
for (View* parent = v->parent(); parent; parent = parent->parent()) {
const gfx::Rect& parent_bounds = parent->GetBoundsInScreen();
location.SetToMax(parent_bounds.origin());
location.SetToMin(parent_bounds.bottom_right());
}
v->ShowContextMenu(location, ui::MENU_SOURCE_KEYBOARD);
event->StopPropagation();
}
}
View* owner_;
DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler);
};
const char RootView::kViewClassName[] = "RootView";
RootView::RootView(Widget* widget)
: widget_(widget),
mouse_pressed_handler_(NULL),
mouse_move_handler_(NULL),
last_click_handler_(NULL),
explicit_mouse_handler_(false),
last_mouse_event_flags_(0),
last_mouse_event_x_(-1),
last_mouse_event_y_(-1),
gesture_handler_(NULL),
scroll_gesture_handler_(NULL),
pre_dispatch_handler_(new internal::PreEventDispatchHandler(this)),
focus_search_(this, false, false),
focus_traversable_parent_(NULL),
focus_traversable_parent_view_(NULL),
event_dispatch_target_(NULL),
old_dispatch_target_(NULL) {
AddPreTargetHandler(pre_dispatch_handler_.get());
SetEventTargeter(scoped_ptr<ui::EventTargeter>(new ViewTargeter()));
}
RootView::~RootView() {
if (has_children())
RemoveAllChildViews(true);
}
void RootView::SetContentsView(View* contents_view) {
DCHECK(contents_view && GetWidget()->native_widget()) <<
"Can't be called until after the native widget is created!";
SetLayoutManager(new FillLayout);
if (has_children())
RemoveAllChildViews(true);
AddChildView(contents_view);
Layout();
}
View* RootView::GetContentsView() {
return child_count() > 0 ? child_at(0) : NULL;
}
void RootView::NotifyNativeViewHierarchyChanged() {
PropagateNativeViewHierarchyChanged();
}
void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) {
DCHECK(focus_traversable != this);
focus_traversable_parent_ = focus_traversable;
}
void RootView::SetFocusTraversableParentView(View* view) {
focus_traversable_parent_view_ = view;
}
void RootView::ThemeChanged() {
View::PropagateThemeChanged();
}
void RootView::LocaleChanged() {
View::PropagateLocaleChanged();
}
FocusSearch* RootView::GetFocusSearch() {
return &focus_search_;
}
FocusTraversable* RootView::GetFocusTraversableParent() {
return focus_traversable_parent_;
}
View* RootView::GetFocusTraversableParentView() {
return focus_traversable_parent_view_;
}
ui::EventTarget* RootView::GetRootTarget() {
return this;
}
ui::EventDispatchDetails RootView::OnEventFromSource(ui::Event* event) {
if (event->IsKeyEvent())
return EventProcessor::OnEventFromSource(event);
else if (event->IsScrollEvent())
DispatchScrollEvent(static_cast<ui::ScrollEvent*>(event));
else if (event->IsTouchEvent())
NOTREACHED() << "Touch events should not be sent to RootView.";
else if (event->IsGestureEvent())
DispatchGestureEvent(static_cast<ui::GestureEvent*>(event));
else if (event->IsMouseEvent())
NOTREACHED() << "Should not be called with a MouseEvent.";
else
NOTREACHED() << "Invalid event type.";
return DispatchDetails();
}
const Widget* RootView::GetWidget() const {
return widget_;
}
Widget* RootView::GetWidget() {
return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget());
}
bool RootView::IsDrawn() const {
return visible();
}
void RootView::Layout() {
View::Layout();
widget_->OnRootViewLayout();
}
const char* RootView::GetClassName() const {
return kViewClassName;
}
void RootView::SchedulePaintInRect(const gfx::Rect& rect) {
if (layer()) {
layer()->SchedulePaint(rect);
} else {
gfx::Rect xrect = ConvertRectToParent(rect);
gfx::Rect invalid_rect = gfx::IntersectRects(GetLocalBounds(), xrect);
if (!invalid_rect.IsEmpty())
widget_->SchedulePaintInRect(invalid_rect);
}
}
bool RootView::OnMousePressed(const ui::MouseEvent& event) {
UpdateCursor(event);
SetMouseLocationAndFlags(event);
if (mouse_pressed_handler_) {
ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
mouse_pressed_handler_);
drag_info_.Reset();
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event);
if (dispatch_details.dispatcher_destroyed)
return true;
return true;
}
DCHECK(!explicit_mouse_handler_);
bool hit_disabled_view = false;
for (mouse_pressed_handler_ = GetEventHandlerForPoint(event.location());
mouse_pressed_handler_ && (mouse_pressed_handler_ != this);
mouse_pressed_handler_ = mouse_pressed_handler_->parent()) {
DVLOG(1) << "OnMousePressed testing "
<< mouse_pressed_handler_->GetClassName();
if (!mouse_pressed_handler_->enabled()) {
hit_disabled_view = true;
break;
}
ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
mouse_pressed_handler_);
if (mouse_pressed_handler_ != last_click_handler_)
mouse_pressed_event.set_flags(event.flags() & ~ui::EF_IS_DOUBLE_CLICK);
drag_info_.Reset();
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event);
if (dispatch_details.dispatcher_destroyed)
return mouse_pressed_event.handled();
if (!mouse_pressed_handler_)
break;
if (mouse_pressed_event.handled()) {
last_click_handler_ = mouse_pressed_handler_;
DVLOG(1) << "OnMousePressed handled by "
<< mouse_pressed_handler_->GetClassName();
return true;
}
}
mouse_pressed_handler_ = NULL;
if (last_click_handler_ && (event.flags() & ui::EF_IS_DOUBLE_CLICK))
hit_disabled_view = true;
last_click_handler_ = NULL;
return hit_disabled_view;
}
bool RootView::OnMouseDragged(const ui::MouseEvent& event) {
if (mouse_pressed_handler_) {
SetMouseLocationAndFlags(event);
ui::MouseEvent mouse_event(event, static_cast<View*>(this),
mouse_pressed_handler_);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_pressed_handler_, &mouse_event);
if (dispatch_details.dispatcher_destroyed)
return false;
}
return false;
}
void RootView::OnMouseReleased(const ui::MouseEvent& event) {
UpdateCursor(event);
if (mouse_pressed_handler_) {
ui::MouseEvent mouse_released(event, static_cast<View*>(this),
mouse_pressed_handler_);
View* mouse_pressed_handler = mouse_pressed_handler_;
SetMouseHandler(NULL);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_pressed_handler, &mouse_released);
if (dispatch_details.dispatcher_destroyed)
return;
}
}
void RootView::OnMouseCaptureLost() {
if (mouse_pressed_handler_ || gesture_handler_) {
if (mouse_pressed_handler_) {
gfx::Point last_point(last_mouse_event_x_, last_mouse_event_y_);
ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
last_point, last_point,
last_mouse_event_flags_,
0);
UpdateCursor(release_event);
}
View* mouse_pressed_handler = mouse_pressed_handler_;
View* gesture_handler = gesture_handler_;
SetMouseHandler(NULL);
if (mouse_pressed_handler)
mouse_pressed_handler->OnMouseCaptureLost();
else
gesture_handler->OnMouseCaptureLost();
}
}
void RootView::OnMouseMoved(const ui::MouseEvent& event) {
View* v = GetEventHandlerForPoint(event.location());
while (v && !v->enabled() && (v != mouse_move_handler_))
v = v->parent();
if (v && v != this) {
if (v != mouse_move_handler_) {
if (mouse_move_handler_ != NULL &&
(!mouse_move_handler_->notify_enter_exit_on_child() ||
!mouse_move_handler_->Contains(v))) {
MouseEnterExitEvent exit(event, ui::ET_MOUSE_EXITED);
exit.ConvertLocationToTarget(static_cast<View*>(this),
mouse_move_handler_);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_move_handler_, &exit);
if (dispatch_details.dispatcher_destroyed)
return;
NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
mouse_move_handler_, v);
}
View* old_handler = mouse_move_handler_;
mouse_move_handler_ = v;
if (!mouse_move_handler_->notify_enter_exit_on_child() ||
!mouse_move_handler_->Contains(old_handler)) {
MouseEnterExitEvent entered(event, ui::ET_MOUSE_ENTERED);
entered.ConvertLocationToTarget(static_cast<View*>(this),
mouse_move_handler_);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_move_handler_, &entered);
if (dispatch_details.dispatcher_destroyed)
return;
NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_ENTERED,
mouse_move_handler_, old_handler);
}
}
ui::MouseEvent moved_event(event, static_cast<View*>(this),
mouse_move_handler_);
mouse_move_handler_->OnMouseMoved(moved_event);
if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT))
widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event));
} else if (mouse_move_handler_ != NULL) {
MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_move_handler_, &exited);
if (dispatch_details.dispatcher_destroyed)
return;
NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
mouse_move_handler_, v);
if (!(event.flags() & ui::EF_IS_NON_CLIENT))
widget_->SetCursor(gfx::kNullCursor);
mouse_move_handler_ = NULL;
}
}
void RootView::OnMouseExited(const ui::MouseEvent& event) {
if (mouse_move_handler_ != NULL) {
MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(mouse_move_handler_, &exited);
if (dispatch_details.dispatcher_destroyed)
return;
NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
mouse_move_handler_, NULL);
mouse_move_handler_ = NULL;
}
}
bool RootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
for (View* v = GetEventHandlerForPoint(event.location());
v && v != this && !event.handled(); v = v->parent()) {
ui::EventDispatchDetails dispatch_details =
DispatchEvent(v, const_cast<ui::MouseWheelEvent*>(&event));
if (dispatch_details.dispatcher_destroyed ||
dispatch_details.target_destroyed) {
return event.handled();
}
}
return event.handled();
}
void RootView::SetMouseHandler(View* new_mh) {
explicit_mouse_handler_ = (new_mh != NULL);
mouse_pressed_handler_ = new_mh;
gesture_handler_ = new_mh;
scroll_gesture_handler_ = new_mh;
drag_info_.Reset();
}
void RootView::GetAccessibleState(ui::AXViewState* state) {
state->name = widget_->widget_delegate()->GetAccessibleWindowTitle();
state->role = widget_->widget_delegate()->GetAccessibleWindowRole();
}
void RootView::UpdateParentLayer() {
if (layer())
ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer());
}
void RootView::DispatchGestureEvent(ui::GestureEvent* event) {
if (gesture_handler_) {
View* handler = scroll_gesture_handler_ &&
(event->IsScrollGestureEvent() || event->IsFlingScrollEvent()) ?
scroll_gesture_handler_ : gesture_handler_;
ui::GestureEvent handler_event(*event, static_cast<View*>(this), handler);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(handler, &handler_event);
if (dispatch_details.dispatcher_destroyed)
return;
if (event->type() == ui::ET_GESTURE_END &&
event->details().touch_points() <= 1) {
if (gesture_handler_ == mouse_pressed_handler_)
SetMouseHandler(NULL);
else
gesture_handler_ = NULL;
}
if (scroll_gesture_handler_ &&
(event->type() == ui::ET_GESTURE_SCROLL_END ||
event->type() == ui::ET_SCROLL_FLING_START)) {
scroll_gesture_handler_ = NULL;
}
if (handler_event.stopped_propagation()) {
event->StopPropagation();
return;
} else if (handler_event.handled()) {
event->SetHandled();
return;
}
if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN &&
!scroll_gesture_handler_) {
for (scroll_gesture_handler_ = gesture_handler_->parent();
scroll_gesture_handler_ && scroll_gesture_handler_ != this;
scroll_gesture_handler_ = scroll_gesture_handler_->parent()) {
ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
scroll_gesture_handler_);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(scroll_gesture_handler_, &gesture_event);
if (gesture_event.stopped_propagation()) {
event->StopPropagation();
return;
} else if (gesture_event.handled()) {
event->SetHandled();
return;
} else if (dispatch_details.dispatcher_destroyed ||
dispatch_details.target_destroyed) {
return;
}
}
scroll_gesture_handler_ = NULL;
}
return;
}
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_UPDATE:
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START:
return;
default:
break;
}
View* gesture_handler = NULL;
if (views::switches::IsRectBasedTargetingEnabled() &&
!event->details().bounding_box().IsEmpty()) {
gfx::Rect touch_rect(event->details().bounding_box());
touch_rect.set_origin(event->location());
touch_rect.Offset(-touch_rect.width() / 2, -touch_rect.height() / 2);
gesture_handler = GetEventHandlerForRect(touch_rect);
} else {
gesture_handler = GetEventHandlerForPoint(event->location());
}
for (gesture_handler_ = gesture_handler;
gesture_handler_ && (gesture_handler_ != this);
gesture_handler_ = gesture_handler_->parent()) {
if (!gesture_handler_->enabled()) {
return;
}
ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
gesture_handler_);
ui::EventDispatchDetails dispatch_details =
DispatchEvent(gesture_handler_, &gesture_event);
if (dispatch_details.dispatcher_destroyed)
return;
if (!gesture_handler_)
return;
if (gesture_event.handled()) {
if (gesture_event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
scroll_gesture_handler_ = gesture_handler_;
if (gesture_event.stopped_propagation())
event->StopPropagation();
else
event->SetHandled();
if (gesture_event.type() == ui::ET_GESTURE_END &&
event->details().touch_points() <= 1) {
gesture_handler_ = NULL;
}
return;
}
}
gesture_handler_ = NULL;
}
void RootView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
widget_->ViewHierarchyChanged(details);
if (!details.is_add) {
if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child)
mouse_pressed_handler_ = NULL;
if (mouse_move_handler_ == details.child)
mouse_move_handler_ = NULL;
if (gesture_handler_ == details.child)
gesture_handler_ = NULL;
if (scroll_gesture_handler_ == details.child)
scroll_gesture_handler_ = NULL;
if (event_dispatch_target_ == details.child)
event_dispatch_target_ = NULL;
if (old_dispatch_target_ == details.child)
old_dispatch_target_ = NULL;
}
}
void RootView::VisibilityChanged(View* , bool is_visible) {
if (!is_visible) {
explicit_mouse_handler_ = false;
mouse_pressed_handler_ = NULL;
mouse_move_handler_ = NULL;
gesture_handler_ = NULL;
scroll_gesture_handler_ = NULL;
event_dispatch_target_ = NULL;
old_dispatch_target_ = NULL;
}
}
void RootView::OnPaint(gfx::Canvas* canvas) {
if (!layer() || !layer()->fills_bounds_opaquely())
canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
View::OnPaint(canvas);
}
gfx::Vector2d RootView::CalculateOffsetToAncestorWithLayer(
ui::Layer** layer_parent) {
gfx::Vector2d offset(View::CalculateOffsetToAncestorWithLayer(layer_parent));
if (!layer() && layer_parent)
*layer_parent = widget_->GetLayer();
return offset;
}
View::DragInfo* RootView::GetDragInfo() {
return &drag_info_;
}
void RootView::DispatchScrollEvent(ui::ScrollEvent* event) {
for (View* v = GetEventHandlerForPoint(event->location());
v && v != this && !event->stopped_propagation(); v = v->parent()) {
ui::EventDispatchDetails dispatch_details = DispatchEvent(v, event);
if (dispatch_details.dispatcher_destroyed ||
dispatch_details.target_destroyed) {
return;
}
}
if (event->handled() || event->type() != ui::ET_SCROLL)
return;
ui::MouseWheelEvent wheel(*event);
if (OnMouseWheel(wheel))
event->SetHandled();
}
void RootView::UpdateCursor(const ui::MouseEvent& event) {
if (!(event.flags() & ui::EF_IS_NON_CLIENT)) {
View* v = GetEventHandlerForPoint(event.location());
ui::MouseEvent me(event, static_cast<View*>(this), v);
widget_->SetCursor(v->GetCursor(me));
}
}
void RootView::SetMouseLocationAndFlags(const ui::MouseEvent& event) {
last_mouse_event_flags_ = event.flags();
last_mouse_event_x_ = event.x();
last_mouse_event_y_ = event.y();
}
void RootView::NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
ui::EventType type,
View* view,
View* sibling) {
for (View* p = view->parent(); p; p = p->parent()) {
if (!p->notify_enter_exit_on_child())
continue;
if (sibling && p->Contains(sibling))
break;
MouseEnterExitEvent notify_event(event, type);
ui::EventDispatchDetails dispatch_details = DispatchEvent(p, ¬ify_event);
if (dispatch_details.dispatcher_destroyed ||
dispatch_details.target_destroyed) {
return;
}
}
}
bool RootView::CanDispatchToTarget(ui::EventTarget* target) {
return event_dispatch_target_ == target;
}
ui::EventDispatchDetails RootView::PreDispatchEvent(ui::EventTarget* target,
ui::Event* event) {
old_dispatch_target_ = event_dispatch_target_;
event_dispatch_target_ = static_cast<View*>(target);
return DispatchDetails();
}
ui::EventDispatchDetails RootView::PostDispatchEvent(ui::EventTarget* target,
const ui::Event& event) {
DispatchDetails details;
if (target != event_dispatch_target_)
details.target_destroyed = true;
event_dispatch_target_ = old_dispatch_target_;
old_dispatch_target_ = NULL;
#ifndef NDEBUG
DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_));
#endif
return details;
}
}
}