OLD | NEW |
(Empty) | |
| 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/aura/window_delegate.h" |
| 12 #include "ui/base/event.h" |
| 13 #include "ui/gfx/path.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // WindowDelegate for capturing drag events in DragDropTracker. This basically |
| 18 // does nothing. |
| 19 class DragDropTrackerWindowDelegate : public aura::WindowDelegate { |
| 20 public: |
| 21 DragDropTrackerWindowDelegate() {} |
| 22 virtual ~DragDropTrackerWindowDelegate() {}; |
| 23 |
| 24 private: |
| 25 // Overridden from WindowDelegate: |
| 26 virtual gfx::Size GetMinimumSize() const OVERRIDE { |
| 27 return gfx::Size(); |
| 28 } |
| 29 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, |
| 30 const gfx::Rect& new_bounds) OVERRIDE { |
| 31 } |
| 32 virtual void OnFocus(aura::Window* old_focused_window) OVERRIDE { |
| 33 } |
| 34 virtual void OnBlur() OVERRIDE { |
| 35 } |
| 36 virtual bool OnKeyEvent(ui::KeyEvent* event) OVERRIDE { |
| 37 return false; |
| 38 } |
| 39 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { |
| 40 return gfx::kNullCursor; |
| 41 } |
| 42 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { |
| 43 return 0; |
| 44 } |
| 45 virtual bool ShouldDescendIntoChildForEventHandling( |
| 46 aura::Window* child, |
| 47 const gfx::Point& location) OVERRIDE { |
| 48 return true; |
| 49 } |
| 50 virtual bool OnMouseEvent(ui::MouseEvent* event) OVERRIDE { |
| 51 return true; |
| 52 } |
| 53 virtual ui::TouchStatus OnTouchEvent(ui::TouchEvent* event) OVERRIDE { |
| 54 return ui::TOUCH_STATUS_END; |
| 55 } |
| 56 virtual ui::GestureStatus OnGestureEvent(ui::GestureEvent* event) OVERRIDE { |
| 57 return ui::GESTURE_STATUS_UNKNOWN; |
| 58 } |
| 59 virtual bool CanFocus() OVERRIDE { return true; } |
| 60 virtual void OnCaptureLost() OVERRIDE {} |
| 61 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
| 62 } |
| 63 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} |
| 64 virtual void OnWindowDestroying() OVERRIDE {} |
| 65 virtual void OnWindowDestroyed() OVERRIDE { delete this; } |
| 66 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} |
| 67 virtual bool HasHitTestMask() const OVERRIDE { return false; } |
| 68 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {} |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(DragDropTrackerWindowDelegate); |
| 71 }; |
| 72 |
| 73 // Creates a window for capturing drag events. |
| 74 aura::Window* CreateCaptureWindow(aura::RootWindow* root_window) { |
| 75 aura::Window* window = new aura::Window(new DragDropTrackerWindowDelegate); |
| 76 window->Init(ui::LAYER_NOT_DRAWN); |
| 77 aura::Window* default_container = |
| 78 ash::Shell::GetContainer(root_window, |
| 79 ash::internal::kShellWindowId_DefaultContainer); |
| 80 default_container->AddChild(window); |
| 81 window->Show(); |
| 82 return window; |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 87 namespace ash { |
| 88 namespace internal { |
| 89 |
| 90 DragDropTracker::DragDropTracker() |
| 91 : capture_window_(NULL) { |
| 92 } |
| 93 |
| 94 DragDropTracker::~DragDropTracker() { |
| 95 StopTracking(); |
| 96 } |
| 97 |
| 98 void DragDropTracker::StartTracking(aura::RootWindow* root_window) { |
| 99 StopTracking(); |
| 100 capture_window_ = CreateCaptureWindow(root_window); |
| 101 capture_window_->SetCapture(); |
| 102 } |
| 103 |
| 104 void DragDropTracker::StopTracking() { |
| 105 if (capture_window_) { |
| 106 capture_window_->ReleaseCapture(); |
| 107 delete capture_window_; |
| 108 } |
| 109 capture_window_ = NULL; |
| 110 } |
| 111 |
| 112 aura::Window* DragDropTracker::GetTarget(const ui::LocatedEvent& event) { |
| 113 std::pair<aura::RootWindow*, gfx::Point> pair = |
| 114 ash::wm::GetRootWindowRelativeToWindow(capture_window_->GetRootWindow(), |
| 115 event.location()); |
| 116 return pair.first->GetEventHandlerForPoint(pair.second); |
| 117 } |
| 118 |
| 119 ui::MouseEvent* DragDropTracker::ConvertMouseEvent( |
| 120 aura::Window* target, |
| 121 const ui::MouseEvent& event) { |
| 122 DCHECK(capture_window_); |
| 123 std::pair<aura::RootWindow*, gfx::Point> location_pair = |
| 124 ash::wm::GetRootWindowRelativeToWindow(capture_window_->GetRootWindow(), |
| 125 event.location()); |
| 126 aura::Window::ConvertPointToTarget(location_pair.first, target, |
| 127 &location_pair.second); |
| 128 std::pair<aura::RootWindow*, gfx::Point> root_location_pair = |
| 129 ash::wm::GetRootWindowRelativeToWindow(capture_window_->GetRootWindow(), |
| 130 event.root_location()); |
| 131 aura::Window::ConvertPointToTarget(root_location_pair.first, target, |
| 132 &root_location_pair.second); |
| 133 return new ui::MouseEvent(event.type(), |
| 134 location_pair.second, |
| 135 root_location_pair.second, |
| 136 event.flags()); |
| 137 } |
| 138 |
| 139 } // namespace internal |
| 140 } // namespace ash |
OLD | NEW |