Home | History | Annotate | Download | only in drag_drop
      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/root_window.h"
     11 #include "ui/base/events/event.h"
     12 #include "ui/gfx/screen.h"
     13 
     14 namespace ash {
     15 namespace internal {
     16 
     17 namespace {
     18 
     19 // Creates a window for capturing drag events.
     20 aura::Window* CreateCaptureWindow(aura::RootWindow* context_root,
     21                                   aura::WindowDelegate* delegate) {
     22   aura::Window* window = new aura::Window(delegate);
     23   window->SetType(aura::client::WINDOW_TYPE_NORMAL);
     24   window->Init(ui::LAYER_NOT_DRAWN);
     25   window->SetDefaultParentByRootWindow(context_root, gfx::Rect());
     26   window->Show();
     27   DCHECK(window->bounds().size().IsEmpty());
     28   return window;
     29 }
     30 
     31 }  // namespace
     32 
     33 DragDropTracker::DragDropTracker(aura::RootWindow* context_root,
     34                                  aura::WindowDelegate* delegate)
     35     : capture_window_(CreateCaptureWindow(context_root, delegate)) {
     36 }
     37 
     38 DragDropTracker::~DragDropTracker()  {
     39   capture_window_->ReleaseCapture();
     40 }
     41 
     42 void DragDropTracker::TakeCapture() {
     43   capture_window_->SetCapture();
     44 }
     45 
     46 aura::Window* DragDropTracker::GetTarget(const ui::LocatedEvent& event) {
     47   DCHECK(capture_window_.get());
     48   gfx::Point location_in_screen = event.location();
     49   wm::ConvertPointToScreen(capture_window_.get(),
     50                            &location_in_screen);
     51   aura::RootWindow* root_window_at_point =
     52       wm::GetRootWindowAt(location_in_screen);
     53   gfx::Point location_in_root = location_in_screen;
     54   wm::ConvertPointFromScreen(root_window_at_point, &location_in_root);
     55   return root_window_at_point->GetEventHandlerForPoint(location_in_root);
     56 }
     57 
     58 ui::LocatedEvent* DragDropTracker::ConvertEvent(
     59     aura::Window* target,
     60     const ui::LocatedEvent& event) {
     61   DCHECK(capture_window_.get());
     62   gfx::Point target_location = event.location();
     63   aura::Window::ConvertPointToTarget(capture_window_.get(), target,
     64                                      &target_location);
     65   gfx::Point location_in_screen = event.location();
     66   ash::wm::ConvertPointToScreen(capture_window_.get(), &location_in_screen);
     67   gfx::Point target_root_location = event.root_location();
     68   aura::Window::ConvertPointToTarget(
     69       capture_window_->GetRootWindow(),
     70       ash::wm::GetRootWindowAt(location_in_screen),
     71       &target_root_location);
     72   return new ui::MouseEvent(event.type(),
     73                             target_location,
     74                             target_root_location,
     75                             event.flags());
     76 }
     77 
     78 }  // namespace internal
     79 }  // namespace ash
     80