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>Table</code>.
27 * <p>Classes that wish to provide their own source image for a <code>Table</code> can
28 * extend the <code>TableDragSourceEffect</code> class, override the
29 * <code>TableDragSourceEffect.dragStart</code> method and set the field
30 * <code>DragSourceEvent.image</code> with their own image.</p>
32 * Subclasses that override any methods of this class must call the corresponding
33 * <code>super</code> method to get the default drag source effect implementation.
35 * @see DragSourceEffect
36 * @see DragSourceEvent
37 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
41 public class TableDragSourceEffect extends DragSourceEffect {
42 Image dragSourceImage = null;
45 * Creates a new <code>TableDragSourceEffect</code> to handle drag effect
46 * from the specified <code>Table</code>.
48 * @param table the <code>Table</code> that the user clicks on to initiate the drag
50 public TableDragSourceEffect(Table table) {
55 * This implementation of <code>dragFinished</code> disposes the image
56 * that was created in <code>TableDragSourceEffect.dragStart</code>.
58 * Subclasses that override this method should call <code>super.dragFinished(event)</code>
59 * to dispose the image in the default implementation.
61 * @param event the information associated with the drag finished event
64 public void dragFinished(DragSourceEvent event) {
65 if (dragSourceImage != null) dragSourceImage.dispose();
66 dragSourceImage = null;
70 * This implementation of <code>dragStart</code> will create a default
71 * image that will be used during the drag. The image should be disposed
72 * when the drag is completed in the <code>TableDragSourceEffect.dragFinished</code>
75 * Subclasses that override this method should call <code>super.dragStart(event)</code>
76 * to use the image from the default implementation.
78 * @param event the information associated with the drag start event
81 public void dragStart(DragSourceEvent event) {
82 event.image = getDragSourceImage(event);
85 Image getDragSourceImage(DragSourceEvent event) {
86 if (dragSourceImage != null) dragSourceImage.dispose();
87 dragSourceImage = null;
88 SHDRAGIMAGE shdi = new SHDRAGIMAGE();
89 int DI_GETDRAGIMAGE = OS.RegisterWindowMessage (new TCHAR (0, "ShellGetDragImage", true)); //$NON-NLS-1$
90 if (OS.SendMessage (control.handle, DI_GETDRAGIMAGE, 0, shdi) != 0) {
91 if ((control.getStyle() & SWT.MIRRORED) != 0) {
92 event.offsetX = shdi.sizeDragImage.cx - shdi.ptOffset.x;
94 event.offsetX = shdi.ptOffset.x;
96 event.offsetY = shdi.ptOffset.y;
97 long hImage = shdi.hbmpDragImage;
99 BITMAP bm = new BITMAP ();
100 OS.GetObject (hImage, BITMAP.sizeof, bm);
101 int srcWidth = bm.bmWidth;
102 int srcHeight = bm.bmHeight;
104 /* Create resources */
105 long hdc = OS.GetDC (0);
106 long srcHdc = OS.CreateCompatibleDC (hdc);
107 long oldSrcBitmap = OS.SelectObject (srcHdc, hImage);
108 long memHdc = OS.CreateCompatibleDC (hdc);
109 BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER ();
110 bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
111 bmiHeader.biWidth = srcWidth;
112 bmiHeader.biHeight = -srcHeight;
113 bmiHeader.biPlanes = 1;
114 bmiHeader.biBitCount = 32;
115 bmiHeader.biCompression = OS.BI_RGB;
116 byte [] bmi = new byte[BITMAPINFOHEADER.sizeof];
117 OS.MoveMemory (bmi, bmiHeader, BITMAPINFOHEADER.sizeof);
118 long [] pBits = new long [1];
119 long memDib = OS.CreateDIBSection (0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0);
120 if (memDib == 0) SWT.error (SWT.ERROR_NO_HANDLES);
121 long oldMemBitmap = OS.SelectObject (memHdc, memDib);
123 BITMAP dibBM = new BITMAP ();
124 OS.GetObject (memDib, BITMAP.sizeof, dibBM);
125 int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight;
127 /* Get the foreground pixels */
128 OS.BitBlt (memHdc, 0, 0, srcWidth, srcHeight, srcHdc, 0, 0, OS.SRCCOPY);
129 byte[] srcData = new byte [sizeInBytes];
130 OS.MoveMemory (srcData, dibBM.bmBits, sizeInBytes);
132 PaletteData palette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
133 ImageData data = new ImageData(srcWidth, srcHeight, bm.bmBitsPixel, palette, bm.bmWidthBytes, srcData);
134 if (shdi.crColorKey == -1) {
135 byte[] alphaData = new byte[srcWidth * srcHeight];
136 int spinc = dibBM.bmWidthBytes - srcWidth * 4;
138 for (int y = 0; y < srcHeight; ++y) {
139 for (int x = 0; x < srcWidth; ++x) {
140 alphaData [ap++] = srcData [sp];
145 data.alphaData = alphaData;
147 data.transparentPixel = shdi.crColorKey << 8;
149 Display display = control.getDisplay();
150 dragSourceImage = new Image(display, new AutoScaleImageDataProvider(display, data, DPIUtil.getDeviceZoom()));
151 OS.SelectObject (memHdc, oldMemBitmap);
152 OS.DeleteDC (memHdc);
153 OS.DeleteObject (memDib);
154 OS.SelectObject (srcHdc, oldSrcBitmap);
155 OS.DeleteDC (srcHdc);
156 OS.ReleaseDC (0, hdc);
157 OS.DeleteObject (hImage);
158 return dragSourceImage;