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 "ui/base/dragdrop/drag_utils.h" | |
6 | |
7 #include <objidl.h> | |
8 #include <shlobj.h> | |
9 #include <shobjidl.h> | |
10 #include <stddef.h> | |
11 | |
12 #include "base/win/scoped_comptr.h" | |
13 #include "base/win/scoped_hdc.h" | |
14 #include "skia/ext/skia_utils_win.h" | |
15 #include "third_party/skia/include/core/SkBitmap.h" | |
16 #include "ui/base/dragdrop/os_exchange_data.h" | |
17 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" | |
18 #include "ui/gfx/canvas.h" | |
19 #include "ui/gfx/geometry/point.h" | |
20 #include "ui/gfx/geometry/size.h" | |
21 #include "ui/gfx/image/image_skia.h" | |
22 #include "ui/gfx/skbitmap_operations.h" | |
23 | |
24 namespace drag_utils { | |
25 | |
26 static void SetDragImageOnDataObject(HBITMAP hbitmap, | |
27 const gfx::Size& size_in_pixels, | |
28 const gfx::Vector2d& cursor_offset, | |
29 IDataObject* data_object) { | |
30 base::win::ScopedComPtr<IDragSourceHelper> helper; | |
31 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, | |
32 IID_IDragSourceHelper, helper.ReceiveVoid()); | |
33 if (SUCCEEDED(rv)) { | |
34 SHDRAGIMAGE sdi; | |
35 sdi.sizeDragImage = size_in_pixels.ToSIZE(); | |
36 sdi.crColorKey = 0xFFFFFFFF; | |
37 sdi.hbmpDragImage = hbitmap; | |
38 sdi.ptOffset = gfx::PointAtOffsetFromOrigin(cursor_offset).ToPOINT(); | |
39 helper->InitializeFromBitmap(&sdi, data_object); | |
40 } | |
41 } | |
42 | |
43 // Blit the contents of the canvas to a new HBITMAP. It is the caller's | |
44 // responsibility to release the |bits| buffer. | |
45 static HBITMAP CreateHBITMAPFromSkBitmap(const SkBitmap& sk_bitmap) { | |
46 base::win::ScopedGetDC screen_dc(NULL); | |
47 BITMAPINFOHEADER header; | |
48 skia::CreateBitmapHeader(sk_bitmap.width(), sk_bitmap.height(), &header); | |
49 void* bits; | |
50 HBITMAP bitmap = | |
51 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), | |
52 DIB_RGB_COLORS, &bits, NULL, 0); | |
53 if (!bitmap || !bits) | |
54 return NULL; | |
55 DCHECK_EQ(sk_bitmap.rowBytes(), static_cast<size_t>(sk_bitmap.width() * 4)); | |
56 SkAutoLockPixels lock(sk_bitmap); | |
57 memcpy( | |
58 bits, sk_bitmap.getPixels(), sk_bitmap.height() * sk_bitmap.rowBytes()); | |
59 return bitmap; | |
60 } | |
61 | |
62 void SetDragImageOnDataObject(const gfx::ImageSkia& image_skia, | |
63 const gfx::Vector2d& cursor_offset, | |
64 ui::OSExchangeData* data_object) { | |
65 DCHECK(data_object && !image_skia.size().IsEmpty()); | |
66 // InitializeFromBitmap() doesn't expect an alpha channel and is confused | |
67 // by premultiplied colors, so unpremultiply the bitmap. | |
68 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. | |
69 HBITMAP bitmap = CreateHBITMAPFromSkBitmap( | |
70 SkBitmapOperations::UnPreMultiply(*image_skia.bitmap())); | |
71 if (bitmap) { | |
72 // Attach 'bitmap' to the data_object. | |
73 SetDragImageOnDataObject( | |
74 bitmap, | |
75 gfx::Size(image_skia.bitmap()->width(), image_skia.bitmap()->height()), | |
76 cursor_offset, | |
77 ui::OSExchangeDataProviderWin::GetIDataObject(*data_object)); | |
78 } | |
79 | |
80 // TODO: the above code is used in non-Ash, while below is used in Ash. If we | |
81 // could figure this context out then we wouldn't do unnecessary work. However | |
82 // as it stands getting this information in ui/base would be a layering | |
83 // violation. | |
84 data_object->provider().SetDragImage(image_skia, cursor_offset); | |
85 } | |
86 | |
87 } // namespace drag_utils | |
OLD | NEW |