Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "ui/aura_shell/drag_drop_controller.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "ui/aura/desktop.h" | |
| 9 #include "ui/aura/window.h" | |
| 10 #include "ui/aura/window_drag_drop_delegate.h" | |
| 11 #include "ui/base/dragdrop/drag_drop_types.h" | |
| 12 #include "ui/base/dragdrop/os_exchange_data_provider_aura.h" | |
| 13 #include "ui/gfx/point.h" | |
| 14 #include "ui/gfx/rect.h" | |
| 15 #include "views/controls/image_view.h" | |
| 16 #include "views/widget/widget.h" | |
| 17 | |
| 18 namespace aura_shell { | |
| 19 namespace internal { | |
| 20 | |
| 21 using aura::Desktop; | |
| 22 | |
| 23 namespace { | |
| 24 using views::Widget; | |
| 25 | |
| 26 Widget* CreateDragWidget() { | |
|
Ben Goodger (Google)
2011/11/11 17:26:53
I'd like it if the whole "view" part of the drag&d
varunjain
2011/11/15 19:39:33
Done.
| |
| 27 Widget* drag_widget = new Widget; | |
| 28 Widget::InitParams params; | |
| 29 params.type = Widget::InitParams::TYPE_TOOLTIP; | |
| 30 params.keep_on_top = true; | |
| 31 params.accept_events = false; | |
| 32 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 33 params.transparent = true; | |
| 34 drag_widget->Init(params); | |
| 35 drag_widget->SetOpacity(0xFF); | |
| 36 return drag_widget; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 DragDropController::DragDropController() | |
| 42 : drag_widget_(NULL), | |
| 43 drag_data_(NULL), | |
| 44 drag_operation_(0), | |
| 45 dragged_window_(NULL), | |
| 46 drag_drop_in_progress_(false), | |
| 47 should_block_during_drag_drop_(true) { | |
| 48 } | |
| 49 | |
| 50 DragDropController::~DragDropController() { | |
| 51 Cleanup(); | |
| 52 } | |
| 53 | |
| 54 void DragDropController::StartDragAndDrop(const ui::OSExchangeData& data, | |
| 55 int operation) { | |
| 56 DCHECK(!drag_drop_in_progress_); | |
| 57 aura::Window* capture_window = Desktop::GetInstance()->capture_window(); | |
| 58 if (capture_window) | |
| 59 Desktop::GetInstance()->ReleaseCapture(capture_window); | |
| 60 drag_drop_in_progress_ = true; | |
| 61 | |
| 62 DCHECK(drag_data_ == NULL && drag_operation_ == 0); | |
| 63 DCHECK(drag_widget_.get() == NULL); | |
| 64 drag_data_ = &data; | |
| 65 drag_widget_.reset(CreateDragWidget()); | |
|
Ben Goodger (Google)
2011/11/11 17:26:53
This would become something like
drag_image_.res
varunjain
2011/11/15 19:39:33
Done.
| |
| 66 drag_operation_ = operation; | |
| 67 gfx::Point location = Desktop::GetInstance()->last_mouse_location(); | |
| 68 const ui::OSExchangeDataProviderAura& provider = | |
| 69 static_cast<const ui::OSExchangeDataProviderAura&>(data.provider()); | |
| 70 | |
| 71 views::ImageView* image_view = new views::ImageView(); | |
| 72 image_view->SetImage(provider.drag_image()); | |
| 73 drag_widget_->SetContentsView(image_view); | |
| 74 drag_widget_->SetBounds(gfx::Rect(location, | |
| 75 image_view->GetPreferredSize())); | |
| 76 drag_widget_->Show(); | |
| 77 | |
| 78 dragged_window_ = Desktop::GetInstance()->GetEventHandlerForPoint(location); | |
| 79 | |
| 80 if (should_block_during_drag_drop_) { | |
| 81 MessageLoopForUI::current()->RunWithDispatcher( | |
| 82 Desktop::GetInstance()->GetDispatcher()); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 void DragDropController::DragUpdate(aura::Window* target, | |
| 87 const aura::MouseEvent& event) { | |
| 88 if (target != dragged_window_) { | |
| 89 if (dragged_window_ && dragged_window_->drag_drop_delegate()) | |
| 90 dragged_window_->drag_drop_delegate()->OnDragExited(); | |
| 91 | |
| 92 dragged_window_ = target; | |
| 93 if (dragged_window_ && dragged_window_->drag_drop_delegate()) { | |
| 94 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); | |
| 95 if (dragged_window_->drag_drop_delegate()->CanDrop(e)) | |
| 96 dragged_window_->drag_drop_delegate()->OnDragEntered(e); | |
| 97 } | |
| 98 } else { | |
| 99 if (dragged_window_ && dragged_window_->drag_drop_delegate()) { | |
| 100 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); | |
| 101 dragged_window_->drag_drop_delegate()->OnDragUpdated(e); | |
| 102 // TODO(varunjain): uncomment the following lines when cursor issue with | |
| 103 // X for tests is fixed. | |
| 104 // gfx::NativeCursor cursor = (op == ui::DragDropTypes::DRAG_NONE)? | |
| 105 // aura::kCursorMove : aura::kCursorHand; | |
| 106 // Desktop::GetInstance()->SetCursor(cursor); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 DCHECK(drag_widget_.get()); | |
| 111 if (drag_widget_->IsVisible()) { | |
| 112 gfx::Rect bounds = drag_widget_->GetClientAreaScreenBounds(); | |
| 113 bounds.set_origin(Desktop::GetInstance()->last_mouse_location()); | |
| 114 drag_widget_->SetBounds(bounds); | |
|
Ben Goodger (Google)
2011/11/11 17:26:53
And this would become drag_image_->SetPosition(Des
varunjain
2011/11/15 19:39:33
Done.
| |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void DragDropController::Drop(aura::Window* target, | |
| 119 const aura::MouseEvent& event) { | |
| 120 DCHECK(target == dragged_window_); | |
| 121 if (dragged_window_ && dragged_window_->drag_drop_delegate() ) { | |
| 122 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); | |
| 123 if (dragged_window_->drag_drop_delegate()->CanDrop(e)) | |
| 124 dragged_window_->drag_drop_delegate()->OnPerformDrop(e); | |
| 125 // TODO(varunjain): else Do drag widget flying back animation | |
| 126 } | |
| 127 | |
| 128 Cleanup(); | |
| 129 if (should_block_during_drag_drop_) | |
| 130 MessageLoop::current()->Quit(); | |
| 131 } | |
| 132 | |
| 133 void DragDropController::DragCancel() { | |
| 134 // TODO(varunjain): Do drag widget flying back animation | |
| 135 Cleanup(); | |
| 136 if (should_block_during_drag_drop_) | |
| 137 MessageLoop::current()->Quit(); | |
| 138 } | |
| 139 | |
| 140 bool DragDropController::IsDragAndDropInProgress() { | |
| 141 return drag_drop_in_progress_; | |
| 142 } | |
| 143 | |
| 144 void DragDropController::Cleanup() { | |
| 145 if (drag_widget_.get()) | |
| 146 drag_widget_->Hide(); | |
|
Ben Goodger (Google)
2011/11/11 17:26:53
This should all be done by drag_image_'s dtor.
varunjain
2011/11/15 19:39:33
Done.
| |
| 147 drag_widget_.reset(); | |
| 148 drag_data_ = NULL; | |
| 149 drag_operation_ = 0; | |
| 150 drag_drop_in_progress_ = false; | |
| 151 } | |
| 152 | |
| 153 } // namespace internal | |
| 154 } // namespace aura_shell | |
| OLD | NEW |