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 "ash/drag_drop/drag_drop_tracker.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/wm/coordinate_conversion.h"
10 #include "ui/aura/client/activation_delegate.h"
11 #include "ui/aura/client/window_tree_client.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/aura/window.h"
14 #include "ui/events/event.h"
15 #include "ui/gfx/screen.h"
22 // An activation delegate which disables activating the drag and drop window.
23 class CaptureWindowActivationDelegate
24 : public aura::client::ActivationDelegate {
26 CaptureWindowActivationDelegate() {}
27 virtual ~CaptureWindowActivationDelegate() {}
29 // aura::client::ActivationDelegate overrides:
30 virtual bool ShouldActivate() const OVERRIDE {
36 DISALLOW_COPY_AND_ASSIGN(CaptureWindowActivationDelegate);
39 // Creates a window for capturing drag events.
40 aura::Window* CreateCaptureWindow(aura::Window* context_root,
41 aura::WindowDelegate* delegate) {
42 static CaptureWindowActivationDelegate* activation_delegate_instance = NULL;
43 if (!activation_delegate_instance)
44 activation_delegate_instance = new CaptureWindowActivationDelegate;
45 aura::Window* window = new aura::Window(delegate);
46 window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
47 window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
48 aura::client::ParentWindowWithContext(window, context_root, gfx::Rect());
49 aura::client::SetActivationDelegate(window, activation_delegate_instance);
51 DCHECK(window->bounds().size().IsEmpty());
57 DragDropTracker::DragDropTracker(aura::Window* context_root,
58 aura::WindowDelegate* delegate)
59 : capture_window_(CreateCaptureWindow(context_root, delegate)) {
62 DragDropTracker::~DragDropTracker() {
63 capture_window_->ReleaseCapture();
66 void DragDropTracker::TakeCapture() {
67 capture_window_->SetCapture();
70 aura::Window* DragDropTracker::GetTarget(const ui::LocatedEvent& event) {
71 DCHECK(capture_window_.get());
72 gfx::Point location_in_screen = event.location();
73 wm::ConvertPointToScreen(capture_window_.get(),
75 aura::Window* root_window_at_point =
76 wm::GetRootWindowAt(location_in_screen);
77 gfx::Point location_in_root = location_in_screen;
78 wm::ConvertPointFromScreen(root_window_at_point, &location_in_root);
79 return root_window_at_point->GetEventHandlerForPoint(location_in_root);
82 ui::LocatedEvent* DragDropTracker::ConvertEvent(
84 const ui::LocatedEvent& event) {
85 DCHECK(capture_window_.get());
86 gfx::Point target_location = event.location();
87 aura::Window::ConvertPointToTarget(capture_window_.get(), target,
89 gfx::Point location_in_screen = event.location();
90 ash::wm::ConvertPointToScreen(capture_window_.get(), &location_in_screen);
91 gfx::Point target_root_location = event.root_location();
92 aura::Window::ConvertPointToTarget(
93 capture_window_->GetRootWindow(),
94 ash::wm::GetRootWindowAt(location_in_screen),
95 &target_root_location);
96 return new ui::MouseEvent(event.type(),
100 static_cast<const ui::MouseEvent&>(event).
101 changed_button_flags());
104 } // namespace internal