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