1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.DPIUtil.*;
20 import org.eclipse.swt.internal.win32.*;
21 import org.eclipse.swt.widgets.*;
24 * This class provides default implementations to display a source image
25 * when a drag is initiated from a <code>Tree</code>.
27 * <p>Classes that wish to provide their own source image for a <code>Tree</code> can
28 * extend <code>TreeDragSourceEffect</code> class and override the <code>TreeDragSourceEffect.dragStart</code>
29 * method and set the field <code>DragSourceEvent.image</code> with their own image.</p>
31 * Subclasses that override any methods of this class must call the corresponding
32 * <code>super</code> method to get the default drag under effect implementation.
34 * @see DragSourceEffect
35 * @see DragSourceEvent
36 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
40 public class TreeDragSourceEffect extends DragSourceEffect {
41 Image dragSourceImage = null;
44 * Creates a new <code>TreeDragSourceEffect</code> to handle drag effect
45 * from the specified <code>Tree</code>.
47 * @param tree the <code>Tree</code> that the user clicks on to initiate the drag
49 public TreeDragSourceEffect(Tree tree) {
54 * This implementation of <code>dragFinished</code> disposes the image
55 * that was created in <code>TreeDragSourceEffect.dragStart</code>.
57 * Subclasses that override this method should call <code>super.dragFinished(event)</code>
58 * to dispose the image in the default implementation.
60 * @param event the information associated with the drag finished event
63 public void dragFinished(DragSourceEvent event) {
64 if (dragSourceImage != null) dragSourceImage.dispose();
65 dragSourceImage = null;
69 * This implementation of <code>dragStart</code> will create a default
70 * image that will be used during the drag. The image should be disposed
71 * when the drag is completed in the <code>TreeDragSourceEffect.dragFinished</code>
74 * Subclasses that override this method should call <code>super.dragStart(event)</code>
75 * to use the image from the default implementation.
77 * @param event the information associated with the drag start event
80 public void dragStart(DragSourceEvent event) {
81 event.image = getDragSourceImage(event);
84 Image getDragSourceImage(DragSourceEvent event) {
85 if (dragSourceImage != null) dragSourceImage.dispose();
86 dragSourceImage = null;
87 SHDRAGIMAGE shdi = new SHDRAGIMAGE();
88 int DI_GETDRAGIMAGE = OS.RegisterWindowMessage (new TCHAR (0, "ShellGetDragImage", true)); //$NON-NLS-1$
89 if (OS.SendMessage (control.handle, DI_GETDRAGIMAGE, 0, shdi) != 0) {
90 if ((control.getStyle() & SWT.MIRRORED) != 0) {
91 event.offsetX = shdi.sizeDragImage.cx - shdi.ptOffset.x;
93 event.offsetX = shdi.ptOffset.x;
95 event.offsetY = shdi.ptOffset.y;
96 long hImage = shdi.hbmpDragImage;
98 BITMAP bm = new BITMAP ();
99 OS.GetObject (hImage, BITMAP.sizeof, bm);
100 int srcWidth = bm.bmWidth;
101 int srcHeight = bm.bmHeight;
103 /* Create resources */
104 long hdc = OS.GetDC (0);
105 long srcHdc = OS.CreateCompatibleDC (hdc);
106 long oldSrcBitmap = OS.SelectObject (srcHdc, hImage);
107 long memHdc = OS.CreateCompatibleDC (hdc);
108 BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER ();
109 bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
110 bmiHeader.biWidth = srcWidth;
111 bmiHeader.biHeight = -srcHeight;
112 bmiHeader.biPlanes = 1;
113 bmiHeader.biBitCount = 32;
114 bmiHeader.biCompression = OS.BI_RGB;
115 byte [] bmi = new byte[BITMAPINFOHEADER.sizeof];
116 OS.MoveMemory (bmi, bmiHeader, BITMAPINFOHEADER.sizeof);
117 long [] pBits = new long [1];
118 long memDib = OS.CreateDIBSection (0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0);
119 if (memDib == 0) SWT.error (SWT.ERROR_NO_HANDLES);
120 long oldMemBitmap = OS.SelectObject (memHdc, memDib);
122 BITMAP dibBM = new BITMAP ();
123 OS.GetObject (memDib, BITMAP.sizeof, dibBM);
124 int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight;
126 /* Get the foreground pixels */
127 OS.BitBlt (memHdc, 0, 0, srcWidth, srcHeight, srcHdc, 0, 0, OS.SRCCOPY);
128 byte[] srcData = new byte [sizeInBytes];
129 OS.MoveMemory (srcData, dibBM.bmBits, sizeInBytes);
131 PaletteData palette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
132 ImageData data = new ImageData(srcWidth, srcHeight, bm.bmBitsPixel, palette, bm.bmWidthBytes, srcData);
133 if (shdi.crColorKey == -1) {
134 byte[] alphaData = new byte[srcWidth * srcHeight];
135 int spinc = dibBM.bmWidthBytes - srcWidth * 4;
137 for (int y = 0; y < srcHeight; ++y) {
138 for (int x = 0; x < srcWidth; ++x) {
139 alphaData [ap++] = srcData [sp];
144 data.alphaData = alphaData;
146 data.transparentPixel = shdi.crColorKey << 8;
148 Display display = control.getDisplay ();
149 dragSourceImage = new Image (display, new AutoScaleImageDataProvider(display, data, DPIUtil.getDeviceZoom()));
150 OS.SelectObject (memHdc, oldMemBitmap);
151 OS.DeleteDC (memHdc);
152 OS.DeleteObject (memDib);
153 OS.SelectObject (srcHdc, oldSrcBitmap);
154 OS.DeleteDC (srcHdc);
155 OS.ReleaseDC (0, hdc);
156 OS.DeleteObject (hImage);
157 return dragSourceImage;