OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/dragdrop/drag_utils.h" | 5 #include "ui/base/dragdrop/drag_utils.h" |
6 | 6 |
7 #include <objidl.h> | 7 #include <objidl.h> |
8 #include <shlobj.h> | 8 #include <shlobj.h> |
9 #include <shobjidl.h> | 9 #include <shobjidl.h> |
10 | 10 |
11 #include "base/win/scoped_comptr.h" | 11 #include "base/win/scoped_comptr.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/base/dragdrop/os_exchange_data.h" | 13 #include "ui/base/dragdrop/os_exchange_data.h" |
14 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" | 14 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" |
15 #include "ui/gfx/canvas.h" | 15 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/gdi_util.h" | 16 #include "ui/gfx/gdi_util.h" |
17 #include "ui/gfx/image/image_skia.h" | 17 #include "ui/gfx/image/image_skia.h" |
18 #include "ui/gfx/skbitmap_operations.h" | 18 #include "ui/gfx/skbitmap_operations.h" |
19 | 19 |
20 namespace drag_utils { | 20 namespace drag_utils { |
21 | 21 |
22 static void SetDragImageOnDataObject(HBITMAP hbitmap, | 22 static void SetDragImageOnDataObject(HBITMAP hbitmap, |
23 const gfx::Size& size, | 23 const gfx::Size& size, |
24 const gfx::Point& cursor_offset, | 24 const gfx::Vector2d& cursor_offset, |
25 IDataObject* data_object) { | 25 IDataObject* data_object) { |
26 base::win::ScopedComPtr<IDragSourceHelper> helper; | 26 base::win::ScopedComPtr<IDragSourceHelper> helper; |
27 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, | 27 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, |
28 IID_IDragSourceHelper, helper.ReceiveVoid()); | 28 IID_IDragSourceHelper, helper.ReceiveVoid()); |
29 if (SUCCEEDED(rv)) { | 29 if (SUCCEEDED(rv)) { |
30 SHDRAGIMAGE sdi; | 30 SHDRAGIMAGE sdi; |
31 sdi.sizeDragImage = size.ToSIZE(); | 31 sdi.sizeDragImage = size.ToSIZE(); |
32 sdi.crColorKey = 0xFFFFFFFF; | 32 sdi.crColorKey = 0xFFFFFFFF; |
33 sdi.hbmpDragImage = hbitmap; | 33 sdi.hbmpDragImage = hbitmap; |
34 sdi.ptOffset = cursor_offset.ToPOINT(); | 34 sdi.ptOffset = gfx::PointAtOffsetFromOrigin(cursor_offset).ToPOINT(); |
35 helper->InitializeFromBitmap(&sdi, data_object); | 35 helper->InitializeFromBitmap(&sdi, data_object); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 // Blit the contents of the canvas to a new HBITMAP. It is the caller's | 39 // Blit the contents of the canvas to a new HBITMAP. It is the caller's |
40 // responsibility to release the |bits| buffer. | 40 // responsibility to release the |bits| buffer. |
41 static HBITMAP CreateHBITMAPFromSkBitmap(const SkBitmap& sk_bitmap) { | 41 static HBITMAP CreateHBITMAPFromSkBitmap(const SkBitmap& sk_bitmap) { |
42 HDC screen_dc = GetDC(NULL); | 42 HDC screen_dc = GetDC(NULL); |
43 BITMAPINFOHEADER header; | 43 BITMAPINFOHEADER header; |
44 gfx::CreateBitmapHeader(sk_bitmap.width(), sk_bitmap.height(), &header); | 44 gfx::CreateBitmapHeader(sk_bitmap.width(), sk_bitmap.height(), &header); |
45 void* bits; | 45 void* bits; |
46 HBITMAP bitmap = | 46 HBITMAP bitmap = |
47 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), | 47 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), |
48 DIB_RGB_COLORS, &bits, NULL, 0); | 48 DIB_RGB_COLORS, &bits, NULL, 0); |
49 DCHECK(sk_bitmap.rowBytes() == sk_bitmap.width() * 4); | 49 DCHECK(sk_bitmap.rowBytes() == sk_bitmap.width() * 4); |
50 SkAutoLockPixels lock(sk_bitmap); | 50 SkAutoLockPixels lock(sk_bitmap); |
51 memcpy( | 51 memcpy( |
52 bits, sk_bitmap.getPixels(), sk_bitmap.height() * sk_bitmap.rowBytes()); | 52 bits, sk_bitmap.getPixels(), sk_bitmap.height() * sk_bitmap.rowBytes()); |
53 ReleaseDC(NULL, screen_dc); | 53 ReleaseDC(NULL, screen_dc); |
54 return bitmap; | 54 return bitmap; |
55 } | 55 } |
56 | 56 |
57 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, | 57 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, |
58 const gfx::Size& size, | 58 const gfx::Size& size, |
59 const gfx::Point& cursor_offset, | 59 const gfx::Vector2d& cursor_offset, |
60 ui::OSExchangeData* data_object) { | 60 ui::OSExchangeData* data_object) { |
61 DCHECK(data_object && !size.IsEmpty()); | 61 DCHECK(data_object && !size.IsEmpty()); |
62 // InitializeFromBitmap() doesn't expect an alpha channel and is confused | 62 // InitializeFromBitmap() doesn't expect an alpha channel and is confused |
63 // by premultiplied colors, so unpremultiply the bitmap. | 63 // by premultiplied colors, so unpremultiply the bitmap. |
64 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. | 64 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. |
65 HBITMAP bitmap = CreateHBITMAPFromSkBitmap( | 65 HBITMAP bitmap = CreateHBITMAPFromSkBitmap( |
66 SkBitmapOperations::UnPreMultiply(*image_skia.bitmap())); | 66 SkBitmapOperations::UnPreMultiply(*image_skia.bitmap())); |
67 | 67 |
68 // Attach 'bitmap' to the data_object. | 68 // Attach 'bitmap' to the data_object. |
69 SetDragImageOnDataObject(bitmap, size, cursor_offset, | 69 SetDragImageOnDataObject(bitmap, size, cursor_offset, |
70 ui::OSExchangeDataProviderWin::GetIDataObject(*data_object)); | 70 ui::OSExchangeDataProviderWin::GetIDataObject(*data_object)); |
71 } | 71 } |
72 | 72 |
73 } // namespace drag_utils | 73 } // namespace drag_utils |
OLD | NEW |