Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / drag_drop / drag_drop_tracker.cc
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.
4
5 #include "ash/drag_drop/drag_drop_tracker.h"
6
7 #include "ash/shell.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"
16
17 namespace ash {
18 namespace internal {
19
20 namespace {
21
22 // An activation delegate which disables activating the drag and drop window.
23 class CaptureWindowActivationDelegate
24     : public aura::client::ActivationDelegate {
25  public:
26   CaptureWindowActivationDelegate() {}
27   virtual ~CaptureWindowActivationDelegate() {}
28
29   // aura::client::ActivationDelegate overrides:
30   virtual bool ShouldActivate() const OVERRIDE {
31     return false;
32   }
33
34  private:
35
36   DISALLOW_COPY_AND_ASSIGN(CaptureWindowActivationDelegate);
37 };
38
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);
50   window->Show();
51   DCHECK(window->bounds().size().IsEmpty());
52   return window;
53 }
54
55 }  // namespace
56
57 DragDropTracker::DragDropTracker(aura::Window* context_root,
58                                  aura::WindowDelegate* delegate)
59     : capture_window_(CreateCaptureWindow(context_root, delegate)) {
60 }
61
62 DragDropTracker::~DragDropTracker()  {
63   capture_window_->ReleaseCapture();
64 }
65
66 void DragDropTracker::TakeCapture() {
67   capture_window_->SetCapture();
68 }
69
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(),
74                            &location_in_screen);
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);
80 }
81
82 ui::LocatedEvent* DragDropTracker::ConvertEvent(
83     aura::Window* target,
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,
88                                      &target_location);
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(),
97                             target_location,
98                             target_root_location,
99                             event.flags(),
100                             static_cast<const ui::MouseEvent&>(event).
101                                 changed_button_flags());
102 }
103
104 }  // namespace internal
105 }  // namespace ash