summaryrefslogtreecommitdiffstats
path: root/views/drag_utils_win.cc
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-28 17:53:36 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-28 17:53:36 +0000
commit9941f620af64aafe096b2c6ba5fcebd03d50031d (patch)
treef77f140accb0765ca6049406b1f498f56d53f85e /views/drag_utils_win.cc
parent540bea826a8942aafaed9e1318e2e3065b0a4ba7 (diff)
downloadchromium_src-9941f620af64aafe096b2c6ba5fcebd03d50031d.zip
chromium_src-9941f620af64aafe096b2c6ba5fcebd03d50031d.tar.gz
chromium_src-9941f620af64aafe096b2c6ba5fcebd03d50031d.tar.bz2
Refactors drag_utils.
BUG=none TEST=none Review URL: http://codereview.chromium.org/113954 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17080 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/drag_utils_win.cc')
-rw-r--r--views/drag_utils_win.cc78
1 files changed, 78 insertions, 0 deletions
diff --git a/views/drag_utils_win.cc b/views/drag_utils_win.cc
new file mode 100644
index 0000000..ed01386
--- /dev/null
+++ b/views/drag_utils_win.cc
@@ -0,0 +1,78 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "views/drag_utils.h"
+
+#include <objidl.h>
+#include <shlobj.h>
+#include <shobjidl.h>
+
+#include "app/gfx/canvas.h"
+#include "app/os_exchange_data.h"
+#include "base/gfx/gdi_util.h"
+
+namespace drag_utils {
+
+static void SetDragImageOnDataObject(HBITMAP hbitmap,
+ int width,
+ int height,
+ int cursor_offset_x,
+ int cursor_offset_y,
+ IDataObject* data_object) {
+ IDragSourceHelper* helper = NULL;
+ HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER,
+ IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper));
+ if (SUCCEEDED(rv)) {
+ SHDRAGIMAGE sdi;
+ sdi.sizeDragImage.cx = width;
+ sdi.sizeDragImage.cy = height;
+ sdi.crColorKey = 0xFFFFFFFF;
+ sdi.hbmpDragImage = hbitmap;
+ sdi.ptOffset.x = cursor_offset_x;
+ sdi.ptOffset.y = cursor_offset_y;
+ helper->InitializeFromBitmap(&sdi, data_object);
+ }
+};
+
+// Blit the contents of the canvas to a new HBITMAP. It is the caller's
+// responsibility to release the |bits| buffer.
+static HBITMAP CreateBitmapFromCanvas(const gfx::Canvas& canvas,
+ int width,
+ int height) {
+ HDC screen_dc = GetDC(NULL);
+ BITMAPINFOHEADER header;
+ gfx::CreateBitmapHeader(width, height, &header);
+ void* bits;
+ HBITMAP bitmap =
+ CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header),
+ DIB_RGB_COLORS, &bits, NULL, 0);
+ HDC compatible_dc = CreateCompatibleDC(screen_dc);
+ HGDIOBJ old_object = SelectObject(compatible_dc, bitmap);
+ BitBlt(compatible_dc, 0, 0, width, height,
+ canvas.getTopPlatformDevice().getBitmapDC(),
+ 0, 0, SRCCOPY);
+ SelectObject(compatible_dc, old_object);
+ ReleaseDC(NULL, compatible_dc);
+ ReleaseDC(NULL, screen_dc);
+ return bitmap;
+}
+
+void SetDragImageOnDataObject(const gfx::Canvas& canvas,
+ int width,
+ int height,
+ int cursor_x_offset,
+ int cursor_y_offset,
+ OSExchangeData* data_object) {
+ DCHECK(data_object && width > 0 && height > 0);
+ // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap.
+ HBITMAP bitmap = CreateBitmapFromCanvas(canvas, width, height);
+
+ // Attach 'bitmap' to the data_object.
+ SetDragImageOnDataObject(bitmap, width, height,
+ cursor_x_offset,
+ cursor_y_offset,
+ data_object);
+}
+
+} // namespace drag_utils