Consolidate bubble border code.
[chromium-blink-merge.git] / ui / views / drag_utils.cc
blobb88e5c1aa8ab7891f9e72ae5bb563cf902bcc4ea
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 "ui/views/drag_utils.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/gfx/size.h"
10 #if defined(USE_AURA)
11 #include "ui/aura/client/drag_drop_client.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/aura/window.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/screen.h"
16 #include "ui/views/widget/widget.h"
17 #elif defined(OS_WIN)
18 #include "ui/base/dragdrop/drag_drop_types.h"
19 #include "ui/base/dragdrop/drag_source.h"
20 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
21 #else
22 #error
23 #endif
25 ui::ScaleFactor GetDeviceScaleFactorForNativeView(views::Widget* widget) {
26 ui::ScaleFactor device_scale_factor = ui::SCALE_FACTOR_100P;
27 #if defined(USE_AURA)
28 // The following code should work on other platforms as well. But we do not
29 // yet care about device scale factor on other platforms. So to keep drag and
30 // drop behavior on other platforms un-touched, we wrap this in the #if guard.
31 if (widget && widget->GetNativeView()) {
32 gfx::Display display = gfx::Screen::GetDisplayNearestWindow(
33 widget->GetNativeView());
34 device_scale_factor = ui::GetScaleFactorFromScale(
35 display.device_scale_factor());
37 #endif
38 return device_scale_factor;
41 namespace views {
43 void RunShellDrag(gfx::NativeView view,
44 const ui::OSExchangeData& data,
45 const gfx::Point& location,
46 int operation) {
47 #if defined(USE_AURA)
48 gfx::Point root_location(location);
49 aura::RootWindow* root_window = view->GetRootWindow();
50 aura::Window::ConvertPointToTarget(view, root_window, &root_location);
51 if (aura::client::GetDragDropClient(root_window)) {
52 aura::client::GetDragDropClient(root_window)->StartDragAndDrop(
53 data, root_window, root_location, operation);
55 #elif defined(OS_WIN)
56 scoped_refptr<ui::DragSource> drag_source(new ui::DragSource);
57 DWORD effects;
58 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
59 drag_source,
60 ui::DragDropTypes::DragOperationToDropEffect(operation),
61 &effects);
62 #endif
65 gfx::Canvas* GetCanvasForDragImage(views::Widget* widget,
66 const gfx::Size& canvas_size) {
67 ui::ScaleFactor device_scale_factor =
68 GetDeviceScaleFactorForNativeView(widget);
69 return new gfx::Canvas(canvas_size, device_scale_factor, false);
72 } // namespace views